如何返回ASP.NET的WebAPI一个文件(FileContentResult) [英] How to return a file (FileContentResult) in ASP.NET WebAPI
问题描述
在一个普通的MVC控制器,我们可以输出PDF文件使用一个 FileContentResult
。
公共FileContentResult测试(TestViewModel VM)
{
VAR流=新的MemoryStream();
......添加内容到流。 返回文件(stream.GetBuffer(),应用程序/ PDF,的test.pdf);
}
但如何才能把它变成一个ApiController?
[HttpPost]
公共IHttpActionResult测试(TestViewModel VM)
{
...
返回OK(pdfOutput);
}
下面是我做过尝试,但它似乎并没有工作。
[HTTPGET]
公共IHttpActionResult测试()
{
VAR流=新的MemoryStream();
...
VAR内容=新StreamContent(流);
content.Headers.ContentType =新MediaTypeHeaderValue(应用程序/ PDF格式);
content.Headers.ContentLength = stream.GetBuffer()长度。
返回OK(内容);
}
在浏览器中显示返回的结果是:
<$p$p><$c$c>{\"Headers\":[{\"Key\":\"Content-Type\",\"Value\":[\"application/pdf\"]},{\"Key\":\"Content-Length\",\"Value\":[\"152844\"]}]}和有一个类似的帖子在这样:<一href=\"http://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api?rq=1\">Returning从控制器的二进制文件中的ASP.NET Web API
。它谈论输出现有文件。但我无法使其与流工作。
有什么建议?
下面是我发现:
而不是返回的 StreamContent
为内容
,我可以把它与 ByteArrayContent工作
。
[HTTPGET]
公众的Htt presponseMessage生成()
{ VAR流=新的MemoryStream();
//处理所述流。 VAR的结果=新的Htt presponseMessage(的HTTPStatus code.OK)
{
内容=新ByteArrayContent(stream.GetBuffer())
};
result.Content.Headers.ContentDisposition =新System.Net.Http.Headers.ContentDispositionHeaderValue(附件)
{
文件名=CertificationCard.pdf
};
result.Content.Headers.ContentType =新MediaTypeHeaderValue(应用程序/八位字节流);
返回结果;
}
In a regular MVC controller, we can output pdf with a FileContentResult
.
public FileContentResult Test(TestViewModel vm)
{
var stream = new MemoryStream();
... add content to the stream.
return File(stream.GetBuffer(), "application/pdf", "test.pdf");
}
But how can we change it into an ApiController?
[HttpPost]
public IHttpActionResult Test(TestViewModel vm)
{
...
return Ok(pdfOutput);
}
Here is what I've tried but it doesn't seem to work.
[HttpGet]
public IHttpActionResult Test()
{
var stream = new MemoryStream();
...
var content = new StreamContent(stream);
content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
content.Headers.ContentLength = stream.GetBuffer().Length;
return Ok(content);
}
The returned result displayed in the browser is:
{"Headers":[{"Key":"Content-Type","Value":["application/pdf"]},{"Key":"Content-Length","Value":["152844"]}]}
And there is a similar post on SO: Returning binary file from controller in ASP.NET Web API . It talks about output an existing file. But I could not make it work with a stream.
Any suggestions?
Here is what I found:
Instead of returning StreamContent
as the Content
, I can make it work with ByteArrayContent
.
[HttpGet]
public HttpResponseMessage Generate()
{
var stream = new MemoryStream();
// processing the stream.
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(stream.GetBuffer())
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "CertificationCard.pdf"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
这篇关于如何返回ASP.NET的WebAPI一个文件(FileContentResult)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!