使用 asp.net core web api,我希望我的控制器动作方法返回一个jpeg图像流。
在我的当前实现中,浏览器仅显示json字符串。 我的期望是在浏览器中看到图片。
使用Chrome开发者工具进行调试时,我发现响应头中仍然返回了
在我的当前实现中,浏览器仅显示json字符串。 我的期望是在浏览器中看到图片。
使用Chrome开发者工具进行调试时,我发现响应头中仍然返回了
Content-Type: application/json; charset=utf-8
[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;
}
using
语句? - developer82return await Task.Run(() => File(image, "image/jpeg"));
为我解决了错误,并且也起作用。 - jman