c# - Web Api 2 - 如何在不保存到磁盘的情况下从 HTTPGET 返回图像(来自 Azure 存储的 MemoryStream)?

标签 c# angularjs asp.net-web-api2 http-get memorystream

我将 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

...和...

Display Image using ashx Handler

关于c# - Web Api 2 - 如何在不保存到磁盘的情况下从 HTTPGET 返回图像(来自 Azure 存储的 MemoryStream)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24985536/

相关文章:

c# - C# 是否内联属性?

c# - WPF Storyboard动画不起作用

angularjs - 有没有办法仅在 ng-view 渲染时渲染 ng-view 之外的 View ?

jquery DataTables 重新加载而不刷新页面

c# - 从 Swagger WebAPI 项目生成 SQL Server 数据库

c# - 如何调用顶级构造函数?

c# - CLR AwareLock::OwnedByCurrentThread 中的访问冲突

javascript - 将驼峰式字符串转换为带连字符的 Angular 服务

AngularJS - 单击按钮时更新动态表中输入框的值

asp.net-mvc - RequestContext.Principal.Identity.Name 在 Web api 2 帖子中为空