本文介绍了Request.Files将同一文件上传为ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
foreach (string fileName in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileName];
//Save file content goes here
fName = file.FileName;
if (file != null && file.ContentLength > 0)
{
subPath = ConfigurationManager.AppSettings["SubPath"].ToString() + "/" + currentUserId;
bool isExists = System.IO.Directory.Exists(Server.MapPath(subPath));
if (!isExists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
string path = System.IO.Path.Combine(Server.MapPath(subPath), System.IO.Path.GetFileName(file.FileName));
file.SaveAs(path);
}
}
如果我上传了多个文件,我会得到n次相同的文件.
If i upload multiple files i get the same file n number of the times.
我正在使用此控件: github/kartik-v/bootstrap-文件输入
我的cs.html
codepen.io/anon/pen/aekqm
请在我的完整代码上方找到.
Please find above my complete code.
推荐答案解决了我的问题:
使用下面的代码
for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; }代替两次获取相同文件的foreach循环
instead of the foreach loop which was taking the same file twice
更多推荐
Request.Files将同一文件上传为ASP.NET MVC
发布评论