求一段上传文件到服务器指定文件夹的代码

来源:百度知道 编辑:UC知道 时间:2024/07/05 17:27:33
要求用input file控件,点一个按钮上传,不要太复杂
能不能把if里面的代码每一句都解释一下 谢谢

页面:
<input id="imgFile" type="file" size="43" runat="server">
后台cs中:

if(this.imgFile.PostedFile.ContentLength>0)
{
//取得文件的全名.包括路径的
string fullname=this.imgFile.PostedFile.FileName;
//取得文件名.不含路径的.
string imagename=System.IO.Path.GetFileName(fullname);
//设置要保存的路径及文件名.
string imagepath=Server.MapPath("路径/你的文件夹名")+"\\"+imagename;
//保存.
this.imgFile.PostedFile.SaveAs(imagepath);
//写入数据库逻辑
}
else
{
this.lblImageMsg.Text="文件不能为空";
return;
}