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类型时,浏览器将作为通用文件下载