【问题标题】:How to return 2 documents (PDF and XFDF) from a C# .NET Core WebAPI controller to a SPA?如何将 2 个文档(PDF 和 XFDF)从 C# .NET Core WebAPI 控制器返回到 SPA?
【发布时间】:2020-05-20 05:38:03
【问题描述】:

我有一个 SPA 和一个 WebAPI。

用户单击 SPA 上的链接,该链接旨在下载 2 个文件(一个 PDF 和一个 XFDF)。


我有这个 WebAPI 操作(来源:What's the best way to serve up multiple binary files from a single WebApi method?

    [HttpGet]
    [Route("/api/files/both/{id}")]
    public HttpResponseMessage GetBothFiles([FromRoute][Required]string id)
    {
        StreamContent pdfContent =null;
        {
            var path = "location of PDF on server";
            var stream = new FileStream(path, FileMode.Open);
            pdfContent = new StreamContent(stream);
            pdfContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/vnd.adobe.xfdf");
        }

        StreamContent xfdfContent = null;
        {
            var path = "location of XFDF on server";
            var stream = new FileStream(path, FileMode.Open);
            xfdfContent = new StreamContent(stream);
            xfdfContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
        }

        var content = new MultipartContent();
        content.Add(pdfContent);
        content.Add(xfdfContent);
        var response = new HttpResponseMessage();
        response.Content = content;
        return response;
    }

在 SPA 中我这样做

window.location.href = "/api/files/both/5";

结果。在浏览器中显示此 JSON

{
    "Version": "1.1",
    "Content": [{
            "Headers": [{
                    "Key": "Content-Type",
                    "Value": ["application/vnd.adobe.xfdf"]
                }
            ]
        }, {
            "Headers": [{
                    "Key": "Content-Type",
                    "Value": ["application/pdf"]
                }
            ]
        }
    ],
    "StatusCode": 200,
    "ReasonPhrase": "OK",
    "Headers": [],
    "TrailingHeaders": [],
    "RequestMessage": null,
    "IsSuccessStatusCode": true
}

响应头是(注意 content-type = application/json)

HTTP/1.1 200 OK
x-powered-by: ASP.NET
content-length: 290
content-type: application/json; charset=utf-8
server: Microsoft-IIS/10.0
request-context: appId=cid-v1:e6b3643a-19a5-4605-a657-5e7333e7b99a
date: Tue, 04 Feb 2020 11:31:49 GMT
connection: close
Vary: Accept-Encoding

原始请求标头(如果感兴趣的话)

Host: localhost:8101
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:8101/
DNT: 1
Connection: keep-alive
Cookie: MySession=....
Upgrade-Insecure-Requests: 1

问题

  1. 如何编写action方法返回2个不同类型的文件?

【问题讨论】:

    标签: javascript c# single-page-application asp.net-core-webapi multipart


    【解决方案1】:

    您不能在同一个请求中返回 2 个不同的文件。您可以将内容嵌入到包含 2 个内容的 json 对象中,但是您必须找到一种显示文件的方法,或者您可以返回 2 个文件的 URI,然后分别对文件发出 2 个请求.

    我个人会选择后一种选择,因为它是最简单和最灵活的,具体取决于您打算如何处理这些文件。

    希望有帮助

    【讨论】:

      【解决方案2】:

      另一种选择是压缩两个/多个文件并将其作为单个文件返回。

      【讨论】:

        猜你喜欢
        • 2023-03-25
        • 1970-01-01
        • 2020-04-25
        • 2012-07-18
        • 1970-01-01
        • 2018-04-09
        • 2023-02-11
        • 2019-04-29
        • 2017-07-27
        相关资源
        最近更新 更多