我正在使用c#和Azure的Web Api 2,并有如何返回图像(从Memorystream的基础)显示在页面上的问题…
这是我的控制器HTTPGET[Route("api/PhotoSubmit/GetPhoto/{id}")]
[HttpGet]
public HttpResponseMessage GetPhotoById(int id)
{
StorageServices storage = new StorageServices();
MemoryStream ms = storage.DownloadBlob(id);
// return what ?
}
这是servicecall的开头:
$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下载二进制图像
……
使用ashx Handler显示图像