Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.net MVC HttpPost中上载大文件_C#_Asp.net_Asp.net Mvc_File_Asp.net Mvc 4 - Fatal编程技术网

C# 如何在ASP.net MVC HttpPost中上载大文件

C# 如何在ASP.net MVC HttpPost中上载大文件,c#,asp.net,asp.net-mvc,file,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,File,Asp.net Mvc 4,我正在尝试在ASP.net MVC中上载大文件,然后在DB中保存。 但当我这样做的时候,我有“outofmemoryexception”。 我在这里发布我的文件: [HttpPost] public void UploadAttachments(int docId, int folderId) { if (Request.Files.Count <= 0) return; //throw exception?

我正在尝试在ASP.net MVC中上载大文件,然后在DB中保存。 但当我这样做的时候,我有“outofmemoryexception”。 我在这里发布我的文件:

[HttpPost]
        public void UploadAttachments(int docId, int folderId)
        {
            if (Request.Files.Count <= 0) return;  //throw exception?
            for (var i = 0; i < Request.Files.Count; i++)
            {
                try
                {
                    var file = Request.Files[i];

                    ProjectAttachmentInput attachment = new ProjectAttachmentInput();
                    attachment = SetAttachment(attachment, file, docId, folderId);
                    _documentationAppService.SaveUploadedFile(attachment);

                }
                catch { }
            }
        }
当我准备好尝试时,我从记忆异常中解脱出来

private static byte[] ReadFully(Stream input)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                input.CopyTo(ms);
                return ms.ToArray();
            }
        }
错误是当我尝试输入时。复制到(毫秒) 我需要文件中的字节[],然后将其保存在DB中。 如果文件小于100MB,则此操作有效。 但当我尝试200MB时,我有一个例外

此代码中应该更改什么

和我的web.config配置:

<system.webServer>
    <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="2147483648" />
    </requestFiltering>
  </security>
...
 <httpRuntime maxRequestLength="1073741824" targetFramework="4.5.1" />

...

首先,如果您希望将大文件上载到您的网站,您应该使用流式IO,并且永远不要将整个文件加载到内存中

其次,在数据库中存储文件不是一个很好的主意,依我看(即使是我自己的观点)

有了这两点,我建议大家:

  • 将文件存储在文件系统中。这可以通过纯流式IO实现,文件大小不会成为问题(只要磁盘未满)
  • 在数据库中,只保留文件哈希

  • “内容可寻址”部分意味着,本质上,每当您将文件保存到文件系统时,首先计算文件的哈希值,然后将该哈希值用作文件名。这将使您实现自动内容重复数据消除,并且您不必依赖于文件名。

    发生这种情况是因为您试图将整个文件加载到内存中的数组中。这里的好做法是使用流,而不是数组,并将文件作为BLOB逐块持久化到数据库中(如果您确实希望将文件存储在数据库中,这是一个有争议的选择)

    但是,为了将流保存为blob,您必须执行额外的步骤

    也许本文可以帮助您(我不知道您使用的是哪种DB)


    可能会对您有所帮助……我想您忘了包含文章的链接了吧?
    <system.webServer>
        <security>
        <requestFiltering>
          <requestLimits maxAllowedContentLength="2147483648" />
        </requestFiltering>
      </security>
    ...
     <httpRuntime maxRequestLength="1073741824" targetFramework="4.5.1" />