我想尝试使用Web API进行rest调用,但我希望响应是存储在数据库中的实际二进制图像,而不是JSON base64编码的字符串。有人对此有一些建议吗?
更新-
这是我最终实现的:
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(new MemoryStream(profile.Avatar));
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "avatar.png";
return result;
最佳答案
您可以将响应内容设置为StreamContent对象:
var fileStream = new FileStream(path, FileMode.Open);
var resp = new HttpResponseMessage()
{
Content = new StreamContent(fileStream)
};
// Find the MIME type
string mimeType = _extensions[Path.GetExtension(path)];
resp.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);
关于asp.net - ASP .Net Web API以二进制形式下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10491845/