使用常规 Controller ,我可以通过返回 FileResult 来做到这一点。这似乎不适用于 ApiController。能做到吗?这是正确的做法吗?
最佳答案
试试这个。
[HttpGet]
public HttpResponseMessage Get()
{
var file = HttpContext.Current.Server.MapPath("~/Images/accent.png");
var stream = new FileStream(file, FileMode.Open);
var content = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(stream)
};
content.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return content;
}
关于asp.net-mvc-4 - 我可以使用MVC4的ApiController实现文件下载吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12014627/