如何使用 ASP.NET 核心创建多部分 HTTP 响应

How to Create a Multipart HTTP Response With ASP.NET Core

我想在我的 ASP.NET 核心控制器中创建一个操作方法,其中 returns 包含多个文件的多部分 HTTP 响应。我知道使用 .zip 文件是网站的推荐方法,但我正在考虑对 API.

使用这样的请求

我在 ASP.NET 核心示例中能够 find 的示例与上传文件时的多部分 HTTP 请求有关。就我而言,我想下载文件。

更新

我提出了以下 GitHub 问题:#4933

来自 MSDN

MSDN has a document that lists a lot of the multipart subtypes. multipart/byteranges 似乎最适合在 HTTP 响应中发送多个文件供客户端应用程序下载。粗体部分特别相关。

The multipart/byteranges content type is defined as a part of the HTTP message protocol. It includes two or more parts, each with its own Content-Type and Content-Range fields. The parts are separated using a MIME boundary parameter. It allows for binary as well as 7-bit and 8-bit files to be sent as multiple parts with the lengths of the parts being specified in the header of each part. Note that while HTTP makes provisions for using MIME for HTTP documents, HTTP is not strictly MIME-compliant. (Emphasis added.)

来自 RFC2068

RFC2068,第19.2节提供了multipart/byteranges的描述。同样,粗体部分是相关的。每个字节范围都可以有自己的 Content-type,结果也可以有自己的 Content-disposition.

The multipart/byteranges media type includes two or more parts, each with its own Content-Type and Content-Range fields. The parts are separated using a MIME boundary parameter. (Emphasis added.)

RFC 也提供了这个技术定义:

Media Type name:           multipart
Media subtype name:        byteranges
Required parameters:       boundary
Optional parameters:       none
Encoding considerations:   only "7bit", "8bit", or "binary" are permitted
Security considerations:   none

RFC 最好的部分是它的示例,下面的 ASP.NET 核心示例对此进行了说明。

HTTP/1.1 206 Partial content
Date: Wed, 15 Nov 1995 06:25:24 GMT
Last-modified: Wed, 15 Nov 1995 04:58:08 GMT
Content-type: multipart/byteranges; boundary=THIS_STRING_SEPARATES

--THIS_STRING_SEPARATES
Content-type: application/pdf
Content-range: bytes 500-999/8000

...the first range...
--THIS_STRING_SEPARATES
Content-type: application/pdf
Content-range: bytes 7000-7999/8000

...the second range
--THIS_STRING_SEPARATES--

请注意,他们发送了两个 PDF!这正是您所需要的。

一个ASP.NET核心方法

这是一个适用于 Firefox 的代码示例。即Firefox下载了三个图片文件,我们可以用Paint打开。 The source is on GitHub

样本使用app.Run()。要使示例适应控制器操作,请将 IHttpContextAccessor 注入控制器并在操作方法中写入 _httpContextAccessor.HttpContext.Response

using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

public class Startup
{
    private const string CrLf = "\r\n";
    private const string Boundary = "--THIS_STRING_SEPARATES";
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.Run(async context =>
        {
            var response = context.Response;
            response.ContentType = $"multipart/byteranges; boundary={Boundary}";

            // TODO Softcode the 'Content-length' header.            
            response.ContentLength = 13646;
            var contentLength = response.ContentLength.Value;

            await response.WriteAsync(Boundary + CrLf);

            var blue = new FileInfo("./blue.jpg");
            var red = new FileInfo("./red.jpg");
            var green = new FileInfo("./green.jpg");

            long start = 0;
            long end = blue.Length;
            await AddImage(response, blue, start, end, contentLength);

            start = end + 1;
            end = start + red.Length;
            await AddImage(response, red, start, end, contentLength);

            start = end + 1;
            end = start + green.Length;
            await AddImage(response, green, start, end, contentLength);

            response.Body.Flush();
        });
    }

    private async Task AddImage(HttpResponse response, FileInfo fileInfo,
        long start, long end, long total)
    {
        var bytes = File.ReadAllBytes(fileInfo.FullName);
        var file = new FileContentResult(bytes, "image/jpg");

        await response
            .WriteAsync($"Content-type: {file.ContentType.ToString()}" + CrLf);

        await response
            .WriteAsync($"Content-disposition: attachment; filename={fileInfo.Name}" + CrLf);

        await response
            .WriteAsync($"Content-range: bytes {start}-{end}/{total}" + CrLf);

        await response.WriteAsync(CrLf);
        await response.Body.WriteAsync(
            file.FileContents,
            offset: 0,
            count: file.FileContents.Length);
        await response.WriteAsync(CrLf);

        await response.WriteAsync(Boundary + CrLf);
    }
}

注意:此示例代码需要重构才能投入生产。

我写了一个更通用的 MultipartResult class 它只是继承自 ActionResult:

使用示例

[Route("[controller]")]
public class MultipartController : Controller
{
    private readonly IHostingEnvironment hostingEnvironment;

    public MultipartController(IHostingEnvironment hostingEnvironment)
    {
        this.hostingEnvironment = hostingEnvironment;
    }

    [HttpGet("")]
    public IActionResult Get()
    {
        return new MultipartResult()
        {
            new MultipartContent()
            {
                ContentType = "text/plain",
                FileName = "File.txt",
                Stream = this.OpenFile("File.txt")
            },
            new MultipartContent()
            {
                ContentType = "application/json",
                FileName = "File.json",
                Stream = this.OpenFile("File.json")
            }
        };
    }

    private Stream OpenFile(string relativePath)
    {
        return System.IO.File.Open(
            Path.Combine(this.hostingEnvironment.WebRootPath, relativePath),
            FileMode.Open,
            FileAccess.Read);
    }
}

实施

public class MultipartContent
{
    public string ContentType { get; set; }

    public string FileName { get; set; }

    public Stream Stream { get; set; }
}

public class MultipartResult : Collection<MultipartContent>, IActionResult
{
    private readonly System.Net.Http.MultipartContent content;

    public MultipartResult(string subtype = "byteranges", string boundary = null)
    {
        if (boundary == null)
        {
            this.content = new System.Net.Http.MultipartContent(subtype);
        }
        else
        {
            this.content = new System.Net.Http.MultipartContent(subtype, boundary);
        }
    }

    public async Task ExecuteResultAsync(ActionContext context)
    {
        foreach (var item in this)
        {
            if (item.Stream != null)
            {
                var content = new StreamContent(item.Stream);

                if (item.ContentType != null)
                {
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(item.ContentType);
                }

                if (item.FileName != null)
                {
                    var contentDisposition = new ContentDispositionHeaderValue("attachment");
                    contentDisposition.SetHttpFileName(item.FileName);
                    content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
                    content.Headers.ContentDisposition.FileName = contentDisposition.FileName;
                    content.Headers.ContentDisposition.FileNameStar = contentDisposition.FileNameStar;
                }

                this.content.Add(content);
            }
        }

        context.HttpContext.Response.ContentLength = content.Headers.ContentLength;
        context.HttpContext.Response.ContentType = content.Headers.ContentType.ToString();

        await content.CopyToAsync(context.HttpContext.Response.Body);
    }
}