如何使用ASP.NET Core创建多部分HTTP响应 [英] How to Create a Multipart HTTP Response With ASP.NET Core

查看:106
本文介绍了如何使用ASP.NET Core创建多部分HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的ASP.NET Core控制器中创建一个操作方法,该方法返回包含多个文件的多部分HTTP响应.我知道对网站推荐使用.zip文件,但我正在考虑对API使用此类请求.

I would like to create an action method in my ASP.NET Core controller which returns a Multipart HTTP Response containing several files. I know that using a .zip file is the recommended approach for websites but I am considering using such a request for an API.

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

The examples I have been able to find in the ASP.NET Core samples are to do with multipart HTTP requests when uploading files. In my case, I want to download files.

更新

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

I've raised the following GitHub issue: #4933

推荐答案

我编写了一个更通用的MultipartResult类,该类仅从ActionResult继承:

I've written a more generic MultipartResult class which just inherits from 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);
    }
}

这篇关于如何使用ASP.NET Core创建多部分HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆