【问题标题】:Content negotiation for File Download In Web APIWeb API 中文件下载的内容协商
【发布时间】:2013-11-08 05:12:02
【问题描述】:

我有一个 Web API REST 服务方法,它返回 pdf 文件。代码如下:

 string content = some byte array;

            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new StringContent(content);
            //a text file is actually an octet-stream (pdf, etc)
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            //we used attachment to force download
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = "mypdf.pdf";
            return result;

我怀疑 API 中的其他方法,我对响应的媒体类型使用了内容协商。我还需要在这里使用内容协商吗?这里需要还是不需要?

【问题讨论】:

  • 我同意下面 Darrel 的观点...还要注意,如果你的内容是字节数组,你可以使用ByteArrayContent

标签: asp.net-web-api content-negotiation


【解决方案1】:

没有。 Conneg 纯粹是可选的。如果您的资源只有 application/pdf 表示,那就这样吧。

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 2014-07-07
    • 2016-09-13
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    相关资源
    最近更新 更多