• <legend id='LZHtv'><style id='LZHtv'><dir id='LZHtv'><q id='LZHtv'></q></dir></style></legend>

    • <bdo id='LZHtv'></bdo><ul id='LZHtv'></ul>

      <small id='LZHtv'></small><noframes id='LZHtv'>

        <tfoot id='LZHtv'></tfoot>
      1. <i id='LZHtv'><tr id='LZHtv'><dt id='LZHtv'><q id='LZHtv'><span id='LZHtv'><b id='LZHtv'><form id='LZHtv'><ins id='LZHtv'></ins><ul id='LZHtv'></ul><sub id='LZHtv'></sub></form><legend id='LZHtv'></legend><bdo id='LZHtv'><pre id='LZHtv'><center id='LZHtv'></center></pre></bdo></b><th id='LZHtv'></th></span></q></dt></tr></i><div id='LZHtv'><tfoot id='LZHtv'></tfoot><dl id='LZHtv'><fieldset id='LZHtv'></fieldset></dl></div>

        在 ASP.NET Core 1.0 上处理大文件上传

        时间:2023-06-08
          <bdo id='gedQD'></bdo><ul id='gedQD'></ul>

        • <small id='gedQD'></small><noframes id='gedQD'>

              <legend id='gedQD'><style id='gedQD'><dir id='gedQD'><q id='gedQD'></q></dir></style></legend>
              <tfoot id='gedQD'></tfoot>
                    <tbody id='gedQD'></tbody>

                • <i id='gedQD'><tr id='gedQD'><dt id='gedQD'><q id='gedQD'><span id='gedQD'><b id='gedQD'><form id='gedQD'><ins id='gedQD'></ins><ul id='gedQD'></ul><sub id='gedQD'></sub></form><legend id='gedQD'></legend><bdo id='gedQD'><pre id='gedQD'><center id='gedQD'></center></pre></bdo></b><th id='gedQD'></th></span></q></dt></tr></i><div id='gedQD'><tfoot id='gedQD'></tfoot><dl id='gedQD'><fieldset id='gedQD'></fieldset></dl></div>
                  本文介绍了在 ASP.NET Core 1.0 上处理大文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当我在 ASP.NET Core 中将大文件上传到我的 web api 时,运行时将在我的处理和存储上传的函数被触发之前将文件加载到内存中.对于大型上传,这成为一个问题,因为它既慢又需要更多内存.对于以前版本的 ASP.NET 有一些文章关于如何禁用请求缓冲,但我找不到任何关于如何使用 ASP.NET Core 执行此操作的信息.是否可以禁用请求缓冲,这样我的服务器上的内存就不会一直用完?

                  When I'm uploading large files to my web api in ASP.NET Core, the runtime will load the file into memory before my function for processing and storing the upload is fired. With large uploads this becomes an issue as it is both slow and requires more memory. For previous versions of ASP.NET there are some articles on how to disable the buffering of requests, but I'm not able to find any information on how to do this with ASP.NET Core. Is it possible to disable the buffering of requests so I don't run out of memory on my server all the time?

                  推荐答案

                  使用 Microsoft.AspNetCore.WebUtilities.MultipartReader 因为它...

                  可以解析任何 [with] 最小缓冲的流.它一次为您提供每个部分的标题和正文,然后您对该部分的正文执行您想要的操作(缓冲、丢弃、写入磁盘等).

                  can parse any stream [with] minimal buffering. It gives you the headers and body of each section one at a time and then you do what you want with the body of that section (buffer, discard, write to disk, etc.).

                  这是一个中间件示例.

                  app.Use(async (context, next) =>
                  {
                      if (!IsMultipartContentType(context.Request.ContentType))
                      {
                          await next();
                          return;
                      }
                  
                      var boundary = GetBoundary(context.Request.ContentType);
                      var reader = new MultipartReader(boundary, context.Request.Body);
                      var section = await reader.ReadNextSectionAsync();
                  
                      while (section != null)
                      {
                          // process each image
                          const int chunkSize = 1024;
                          var buffer = new byte[chunkSize];
                          var bytesRead = 0;
                          var fileName = GetFileName(section.ContentDisposition);
                  
                          using (var stream = new FileStream(fileName, FileMode.Append))
                          {
                              do
                              {
                                  bytesRead = await section.Body.ReadAsync(buffer, 0, buffer.Length);
                                  stream.Write(buffer, 0, bytesRead);
                  
                              } while (bytesRead > 0);
                          }
                  
                          section = await reader.ReadNextSectionAsync();
                      }
                  
                      context.Response.WriteAsync("Done.");
                  });
                  

                  这里是帮手.

                  private static bool IsMultipartContentType(string contentType)
                  {
                      return 
                          !string.IsNullOrEmpty(contentType) &&
                          contentType.IndexOf("multipart/", StringComparison.OrdinalIgnoreCase) >= 0;
                  }
                  
                  private static string GetBoundary(string contentType)
                  {
                      var elements = contentType.Split(' ');
                      var element = elements.Where(entry => entry.StartsWith("boundary=")).First();
                      var boundary = element.Substring("boundary=".Length);
                      // Remove quotes
                      if (boundary.Length >= 2 && boundary[0] == '"' && 
                          boundary[boundary.Length - 1] == '"')
                      {
                          boundary = boundary.Substring(1, boundary.Length - 2);
                      }
                      return boundary;
                  }
                  
                  private string GetFileName(string contentDisposition)
                  {
                      return contentDisposition
                          .Split(';')
                          .SingleOrDefault(part => part.Contains("filename"))
                          .Split('=')
                          .Last()
                          .Trim('"');
                  }
                  

                  外部参考

                  • https://github.com/aspnet/HttpAbstractions/pull/146
                  • https://github.com/aspnet/HttpAbstractions

                  这篇关于在 ASP.NET Core 1.0 上处理大文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ASP.NET Core 等效于 ASP.NET MVC 5 的 HttpException 下一篇:覆盖 ASP.NET Core 1.0 MVC 中的全局授权过滤器

                  相关文章

                  1. <i id='Z27MO'><tr id='Z27MO'><dt id='Z27MO'><q id='Z27MO'><span id='Z27MO'><b id='Z27MO'><form id='Z27MO'><ins id='Z27MO'></ins><ul id='Z27MO'></ul><sub id='Z27MO'></sub></form><legend id='Z27MO'></legend><bdo id='Z27MO'><pre id='Z27MO'><center id='Z27MO'></center></pre></bdo></b><th id='Z27MO'></th></span></q></dt></tr></i><div id='Z27MO'><tfoot id='Z27MO'></tfoot><dl id='Z27MO'><fieldset id='Z27MO'></fieldset></dl></div>

                    <small id='Z27MO'></small><noframes id='Z27MO'>

                    <legend id='Z27MO'><style id='Z27MO'><dir id='Z27MO'><q id='Z27MO'></q></dir></style></legend>

                      <bdo id='Z27MO'></bdo><ul id='Z27MO'></ul>

                    1. <tfoot id='Z27MO'></tfoot>