您的位置:首页 > 其它

type="file" 文件上传 取不到值解决方案

2012-05-29 11:26 260 查看
原文链接

提起文件上传,对于我这样的新手来说可真是历尽了百般折磨,最后终于佳果。呵呵,我只是小感慨一下,不废话了。

你首先可能想到的用asp.net控件“FileUpload”,这是个好办法,不过我想对于老手来说就不然了,因为用服务器端控件灵活性不好。我个人就偏向于用客户端控件<input type="file" name="fileUpload" id="fileUplaod" >.

用客户端控件时要注意:

首先<form/>的属性一定要设成:enctype="multipart/form-data";

其次(很重要奥!)<input type="file" name="xxxxxxx"/>一定不要忘记“name”属性。否则在后台代码中用Request.Files是取不到值得!

具体代码如下:

前台代码:

<form name="uploadForm" method="post" action="Export.aspx" enctype="multipart/form-data" id="uploadForm">
<input type="file" name="fileUpload" id="fileUpload"/><br />
<input type="submit" id="btnUpload" name="btnUpload" value="上传文件"/>
</form>

后台代码:

System.Web.HttpFileCollection files = Request.Files;
for (int fileCount = 0; fileCount < files.Count; fileCount++)
{
System.Web.HttpPostedFile postedfile = files[fileCount];

string fileName = System.IO.Path.GetFileName(postedfile.FileName);
if (!String.IsNullOrEmpty(fileName))
{

string fileExtension = System.IO.Path.GetExtension(fileName);    //获取文件类型
postedfile.SaveAs(Server.MapPath("/UpLoadFiles/") + fileName);//上传到服务器的路径
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐