【发布时间】:2017-01-03 19:00:45
【问题描述】:
我需要使用 Web API Get 方法返回图像。下面的代码似乎工作正常,除了我在 Fiddler 的 ImageView 窗口中收到此消息,“此响应已编码,但不声称是图像。”
public HttpResponseMessage Get()
{
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StreamContent(fs);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
return response;
}
}
我在 Fiddler 中也看到了与此代码相同的结果:
public HttpResponseMessage Get()
{
HttpResponseMessage response = new HttpResponseMessage();
Byte[] b = (GetImageByteArray());
response.Content = new ByteArrayContent(b);
response.Content.LoadIntoBufferAsync(b.Length).Wait();
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
return response;
}
如果我使用 .png 格式,我会得到相同的结果。
感谢您的帮助,
【问题讨论】:
-
您能否提供有关 GetImageByteArray() 方法的详细信息?您还确定您正在阅读的图像是 jpeg / jpg 图像而不是其他格式的图像吗?
标签: c# asp.net-web-api asp.net-core