
单个文件的上传:
保存到上传服务器指定目录: FileUpload1.Save(Server.MapPath("/upfiles/upload/") +FileUpload1.FileName);
得到上传文件的文件名(含上传本地路径):FileUpload1.PostedFile.FileName;
得到上传文件的大小:FileUpload1.PostedFile.ContentLength;
得到上传文件上传类型:FileUpload1.PostedFile.ContentType;
得到上传文件扩展名:System.IO.Path.GetExtension(FileUpload1.FileName);
得到上传文件名:FileUpload1.FileName;
同时多个文件的上传:
方法是将 System.IO 类导入到 ASP.NET 页中,然后使用 HttpFileCollection 类捕获通过 Request 对象发送来的所有文件。该方法使您可以从一个页面上载所需数量的文件。
使用 HttpFileCollection 类和 Request.Files 属性使您可以控制从该页上载的所有文件。
(你可以在上传页面上放N个FileUpload控件)
得到上传的文件名:System.IO.Path.GetFileName(FileUpload1.FileName);//Request.Files得到的多部分MIME格式的由客户端上载的文件的集合都是包含上传本地完整路径的。
protected void Button1_Click(object sender, EventArgs e)
{
string filepath = Server.MapPath("/upfiles/upload/") ;
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{ HttpPostedFile userPostedFile = uploadedFiles[i];
try { if (userPostedFile.ContentLength > 0 )
{ Label1.Text += "File #" + (i+1) + ""; Label1.Text += "File Content Type: " + userPostedFile.ContentType + ""; Label1.Text += "File Size: " + userPostedFile.ContentLength + "kb"; Label1.Text += "File Name: " + userPostedFile.FileName + ""; userPostedFile.SaveAs(filepath + "\\\\" + System.IO.Path.GetFileName(userPostedFile.FileName)); Label1.Text += "Location where saved: " + filepath + "\\\\" + System.IO.Path.GetFileName(userPostedFile.FileName) + ""; }
} catch (Exception Ex)
{ Label1.Text += "Error: " + Ex.Message; }
}
}
