【问题标题】:Web API 2 C# how to render a file in a web pageWeb API 2 C#如何在网页中呈现文件
【发布时间】:2021-01-16 23:54:48
【问题描述】:

当我向这个特定句柄发送请求时,使用以下代码在网页中呈现文件

[HttpGet]
[Route("learning/{pdfid}")]

public HttpResponseMessage RenderProjectDocumentById(string pdfid)
{
    var doc = _projectDocumentService.GetProjectDocument(pdfid);
    var contentType = "application/octet-stream";
    var res = Request.CreateResponse(HttpStatusCode.OK);
    res.Content = new StreamContent(new MemoryStream(doc.BinaryData));
    res.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    res.Content.Headers.ContentDisposition.FileName = doc.name + "." + doc.doc;
    res.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
    return res;
}

如果我在网络浏览器中输入这个

http://localhost:49454/learning/5711130b9aa56d03a0e2f26f

它会在自己的网页中下载文件

但如果我从 WPF 应用程序发送相同的请求,如下所示

var response = await Client.GetAsync("/learning/" + doc.Id);

如何让它从 html 页面下载?

【问题讨论】:

    标签: c# wpf asp.net-web-api asp.net-web-api2


    【解决方案1】:

    尝试将内容类型从 application/octet-stream 更改为 application/pdf

    当 application/octet-stream 被用作 MIME 类型时,浏览器将作为通用文件下载。

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      相关资源
      最近更新 更多