我将 Web Api 2 与 C# 和 Azure 一起使用,并且在如何返回图像(来自 Memorystream 的基础)以显示在页面上时遇到问题...
这是我的 Controller HTTPGET
[Route("api/PhotoSubmit/GetPhoto/{id}")]
[HttpGet]
public HttpResponseMessage GetPhotoById(int id)
{
StorageServices storage = new StorageServices();
MemoryStream ms = storage.DownloadBlob(id);
// return what ?
}
这是服务调用的开始:
$http({
method: 'GET',
url: 'api/PhotoSubmit/GetPhoto/' + $routeParams.id,
accept: 'application/json'
})
.success(function(result) {
// How do i handle the result and what HTML should i use ? <img ?
});
最佳答案
从客户端你不需要使用 $http。您可以使用普通的旧 HTML 来简化该过程...
<img src="/api/PhotoSubmit/GetPhoto/2232" />
对于动态图像,像这样使用 JQuery...
$('#ImageLocation').html('<img src="/api/PhotoSubmit/GetPhoto/' + intID + '" />');
Web 浏览器将自动执行为图像发出 HTTP 请求的工作,为您省去所有的复杂性。
在服务器端,您可以使用类似这些的过程来加载文件并将其流式传输到客户端。服务器代码返回正确的 MIME 类型很重要,例如...
context.Response.ContentType = "image/png";
资源:
ASP .Net Web API downloading images as binary
...和...
关于c# - Web Api 2 - 如何在不保存到磁盘的情况下从 HTTPGET 返回图像(来自 Azure 存储的 MemoryStream)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24985536/