mal*_*s98 3 asp.net-mvc file-upload
我正在ASP.Net MVC中开发一个表单,其中包含一个表单中<input type="file" />不强制的表单.
在我的控制器中,我有这个代码:
[AcceptVerbs(HttpVerbs.Post)]
[ValidateInput(false)]
public ActionResult Create(FormCollection collection) {
...
//get uploaded file
if (Request.Files.Count > 0)
{
file = Request.Files["imgFileUpload"];
if (file.ContentLength == 0)
{
throw new InvalidOperationException("File contents are empty");
}
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
现在,当用户上传没有内容的文件时,表单会引发异常并提示用户 - 这是预期的.
现在,如果用户决定不再上传任何文件并单击"提交"按钮,则仍会显示上一个异常,这非常棒.
我已经尝试检查值Request.Files并注意到它的计数仍然设置为1 ...它仍然认为那里有一个文件,因为用户没有在<input type="file" />控件上放置任何文件.
有没有人遇到过这个?如果是这样,您采取了哪些措施来防止再次重新执行先前的异常?
非常感谢!
我刚刚在类似的场景中遇到过同样的问题,我做了以下修复:
if(Request.Files.Count > 0)
{
if(Request.Files[0].ContentLength > 0)
{
//Upload the file...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3714 次 |
| 最近记录: |