【发布时间】:2017-04-09 05:20:10
【问题描述】:
使用 asp.net core web api,我想让我的控制器操作方法返回一个 jpeg 图像流。
在我当前的实现中,浏览器只显示一个 json 字符串。
我的期望是在浏览器中看到图像。
使用chrome开发者工具调试的时候发现内容类型还是
Content-Type:application/json; charset=utf-8
在响应标头中返回,即使在我的代码中我手动将内容类型设置为“image/jpeg”。
寻找解决方案我的 Web API 如下
[HttpGet]
public async Task<HttpResponseMessage> Get()
{
var image = System.IO.File.OpenRead("C:\\test\random_image.jpeg");
var stream = new MemoryStream();
image.CopyTo(stream);
stream.Position = 0;
result.Content = new StreamContent(image);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "random_image.jpeg";
result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
result.Content.Headers.ContentLength = stream.Length;
return result;
}
【问题讨论】:
标签: asp.net-core content-type asp.net-core-webapi