【问题标题】:Error message not shown if Fileupload size is more than 5 MB如果 Fileupload 大小超过 5 MB,则不会显示错误消息
【发布时间】:2016-04-06 05:21:48
【问题描述】:

我有一个文件上传页面,在单击按钮时我正在调用一种方法,其中我将最大大小定义为 5 MB。但是当我上传更多内容时,不会显示大小错误消息。这是我的代码:

public ActionResult document(HttpPostedFileBase file, Document model)
{
    tbldocument doc = new tbldocument();
    //Document model = new Document();
    doc.DocumentName = model.documentname;

    if (ModelState.IsValid)
    {
        if (file == null)
        {
            ModelState.AddModelError("File", "Please Upload Your file");
        }
        else if (file.ContentLength > 0)
        {
            int MaxContentLength = 1024 * 1024 * 10; //10 MB
            string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".pdf", ".docx", ".doc", ".xml", ".odt", ".xlsx", ".ppt" };

            if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
            {
                ModelState.AddModelError("File", "Please file of type: " + string.Join(", ", AllowedFileExtensions));
            }

            else if (file.ContentLength > MaxContentLength)
            {
                ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB");
            }
            else
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Server.MapPath("~/App_Data/uploads/documents/" + model.projectid + "");
                //var pathFile = path + fileName;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                var pathWithfileName = Path.Combine(path, fileName);
                file.SaveAs(pathWithfileName);
                ModelState.Clear();
                tbldocument objdb = new tbldocument();
                objdb.DocumentName = model.documentname;
                objdb.ProjectId = model.projectid;
                objdb.Path = model.path;
                objdb.Path = Convert.ToString(pathWithfileName);
                // objdb.FileName = file.FileName;
                //objdb.FileName = file.FileName;
                objdoc.upload(objdb);
                //" + projectid + "
                ViewBag.Message = "File uploaded successfully. File path : ~/Upload/" + fileName;
            }
        }
    }
    return RedirectToAction("ProjectTask", "TblTask");
}

这是快照。它没有向用户显示文件必须小于 5 MB 的消息,而是引发如下错误:

【问题讨论】:

  • 感谢您编辑@Stephen Muecke
  • 该方法是否标有[HttpPost]?在您的代码中,您永远不会返回视图(您只是重定向),因此添加 ModelSateErrorViewBag 属性有点毫无意义
  • 那我应该返回模型还是文件?我重定向是因为当用户点击上传时,我将重定向到不在同一页面上的文档列表或项目列表
  • 我改为从重定向返回视图但同样的错误
  • 查看其他答案以获得可能的解决方案。我的评论与您代码中的其他错误有关。如果你添加了一个 ModelStateError, then you need to check again for ModelState` 是无效的,如果是这样,返回视图(你不能调用ModelState.Clear();,因为这只会清除你刚刚添加的所有错误)

标签: c# .net asp.net-mvc localhost


【解决方案1】:

由于在文件完全上传之前上传存储在 IIS RAM 中,因此当文件太大时,IIS 会在调用您的方法之前停止。如果您想上传大文件,请在 web.config 中添加类似这样的内容:

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="104857600" /><!-- bytes -->
        </requestFiltering>
    </security>
</system.webServer>

如果您想更优雅地处理这些“未处理”错误,请在 global.asax.cs 中添加 Application_Error 方法。

【讨论】:

  • 你能更详细地解释一下@robrich 吗?
  • 哪一部分让你感到困惑?
  • 我在 webconfig 中复制了你的代码,但同样的错误
  • 查看 msdn.microsoft.com/en-us/library/24395wz3.aspx 以获取 global.asax.cs 中 Application_Error 方法的示例
【解决方案2】:

您可能会遇到 ASP.NET 中的默认请求限制,即:4MB (docs)。

发件人:http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/

此外,ASP.NET 运行时有自己的文件大小限制(这是 也被 IIS 6 使用,所以如果你使用那个版本,那是唯一的地方 您需要修改) – 位于 httpRuntime 元素下 web.config。

默认设置为 4MB (4096kB)。你也需要改变它 (它以 kB 为单位而不是之前的字节设置!):

<system.web>
  <httpRuntime maxRequestLength="2097152" />
</system.web>

值得记住的是,在这两个配置元素中, 以较小的请求长度限制为准。

此外,鉴于您将应用程序托管在 IIS 7 或更高版本中,您还应该记得调整 maxAllowedContentLength(此设置由 IIS 使用)。默认值为 30000000 字节 (~28.6 MB) (docs)。

我可以鼓励您看看:http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/ - 那篇文章很好地介绍了如何在 ASP.NET 中处理大文件。

【讨论】:

  • 没听懂你先生,你能@Lasse Christiansen 告诉我我到底能做什么?
  • &lt;system.web&gt;&lt;httpRuntime ... 用于 IIS 6 及更低版本,&lt;system.webServer&gt;&lt;security&gt;... 用于 IIS 7+。除非您使用的是 Win XP 或 Server 2003,否则此解决方案可能不再对您有帮助。
  • @robrich 感谢您的评论 :) 我想知道您是否有一些说明httpRuntime 用于 IIS 6 及以下版本的文档?据我了解,IIS 7+ 有自己的设置 (maxAllowedContentLength) 而 ASP.NET 仍然依赖于maxRequestLength?据我了解,不同之处在于 IIS 6 使用与 ASP.NET 相同的设置。不过我可能错了:)
  • 谢谢@robrich - 正如我所读到的,这并不是说httpRuntime 不是ASP.NET 需要/使用的吗? :) 正如在其他地方看到的那样,比如这里:stackoverflow.com/a/3853785/700926 - 两个值都必须存在才能配置 IIS 和 ASP.NET。还是我错过了什么? :)
  • 我读它的方式是 中使用的所有 IIS 和/或 ASP.NET 在 IIS 7 中被 中的东西以及 IIS 和 ASP 所取代。 NET 开始在那里阅读它们。我读错了吗?唉,我通常将这两个地方与 一起设置,意思是“不要对重复感到厌烦”。
猜你喜欢
  • 2014-05-01
  • 2013-08-26
  • 2016-07-08
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
相关资源
最近更新 更多