Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Web API 2 C#如何在网页中呈现文件_C#_Wpf_Asp.net Web Api_Asp.net Web Api2 - Fatal编程技术网

Web API 2 C#如何在网页中呈现文件

Web API 2 C#如何在网页中呈现文件,c#,wpf,asp.net-web-api,asp.net-web-api2,C#,Wpf,Asp.net Web Api,Asp.net Web Api2,当我向这个特定句柄发送请求时,使用以下代码在网页中呈现文件 [HttpGet] [Route("learning/{pdfid}")] public HttpResponseMessage RenderProjectDocumentById(string pdfid) { var doc = _projectDocumentService.GetProjectDocument(pdfid); var contentType = "applicati

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

[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;
}
如果我在web浏览器中键入此

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

它将在自己的网页上下载文件

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

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

如何使其从html页面下载?

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

当应用程序/八位字节流用作MIME类型时,浏览器将作为通用文件下载