我想尝试使用Web API进行休息调用,但我想让响应是存储在数据库中的实际二进制映像,而不是JSON base64编码字符串。任何人都有一些指针?
更新 – @H_502_3@这是我最后实现:
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);