处理ASP.NET Core 1.0上的大型文件上载

时间:2016-04-05 21:21:09

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

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

3 个答案:

答案 0 :(得分:23)

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

  

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

这是一个中间件示例。

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('"');
}

外部参考

答案 1 :(得分:2)

Controller中,您只需使用Request.Form.Files即可访问文件:

[HttpPost("upload")]
public async Task<IActionResult> UploadAsync(CancellationToken cancellationToken)
{
    if (!Request.HasFormContentType)
        return BadRequest();

    var form = Request.Form;
    foreach(var formFile in form.Files)
    {
        using(var readStream = formFile.OpenReadStream())
        {
            // Do something with the uploaded file
        }
    }


    return Ok();
}

答案 2 :(得分:1)

Shaun Luttin的回答很好,现在他展示的大部分工作都由 ASP.Net Core 2.2 提供。

获取边界:

// Microsoft.AspNetCore.Http.Extensions.HttpRequestMultipartExtensions
var boundary = Request.GetMultipartBoundary();

if (string.IsNullOrWhiteSpace(boundary))
  return BadRequest();

您仍然可以看到以下部分:

var reader = new MultipartReader(boundary, Request.Body);
var section = await reader.ReadNextSectionAsync();

检查配置并转换为FileMultipartSection:

if (section.GetContentDispositionHeader())
{
     var fileSection = section.AsFileSection();
     var fileName = fileSection.FileName;

     using (var stream = new FileStream(fileName, FileMode.Append))
         await fileSection.FileStream.CopyToAsync(stream);
}