如何在ASP.NET WebAPI中返回文件(FileContentResult)
在常规的MVC控制器中,我们可以用a输出pdf FileContentResult。
public FileContentResult Test(TestViewModel vm){
var stream = new MemoryStream();
//... add content to the stream.
return File(stream.GetBuffer(), "application/pdf", "test.pdf");}但是我们怎样才能把它变成一个ApiController?
[HttpPost]public IHttpActionResult Test(TestViewModel vm){
//...
return Ok(pdfOutput);}这是我尝试过但它似乎不起作用。
[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); }浏览器中显示的返回结果为:
{"Headers":[{"Key":"Content-Type","Value":["application/pdf"]},{"Key":"Content-Length","Value":["152844"]}]}有什么建议?
杨__羊羊
相关分类