【问题标题】:ASP.NET MVC 5, return image from BLOBASP.NET MVC 5,从 BLOB 返回图像
【发布时间】:2015-12-19 20:37:41
【问题描述】:

由于某些设备无法访问 BLOB 的域,因此我必须使用我的 Portal(ASP.NET MVC 5) 来路由请求。代码如下:

public class ImageController : Controller
{
    [HttpGet]
    public async Task<HttpResponseMessage> GetImage()
    {
        UriBuilder uriBuilder = new UriBuilder()
        {
            Scheme = "https",
            Host = "portalvhdszhm9fnx146yln.blob.core.windows.net",
            Path = "ads/logo.jpg"
        };

        using (HttpClient client = new HttpClient())
        {
            return await client.GetAsync(uriBuilder.ToString());
        }
    }
}

我的意图是让控制器成为 Blob 文件的路由器。但控制器返回以下内容:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RTpcTXlQcm9qXEF1dGhDbG91ZFxBdXRoQ2xvdWRQb3J0YWxcaW1hZ2VcZ2V0aW1hZ2U=?=
X-Powered-By: ASP.NET
Date: Tue, 22 Sep 2015 17:21:02 GMT
Content-Length: 539

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  x-ms-request-id: 3fad2a06-0001-004b-7f5b-f549ad000000
  x-ms-version: 2009-09-19
  x-ms-lease-status: unlocked
  x-ms-blob-type: BlockBlob
  Date: Tue, 22 Sep 2015 17:21:01 GMT
  ETag: 0x8D2BB762EED052B
  Server: Windows-Azure-Blob/1.0
  Server: Microsoft-HTTPAPI/2.0
  Content-Length: 27780
  Content-MD5: pddt6QJK1FjJgiMTp5HKGQ==
  Content-Type: application/octet-stream
  Last-Modified: Sat, 12 Sep 2015 13:29:28 GMT
}

我想要的是在需要控制器时显示 blob 图像。正确的代码是什么?

【问题讨论】:

  • 您是否尝试过返回FileResult
  • 能否提供一些返回FileResult的示例代码?

标签: asp.net-mvc image asp.net-mvc-5 blob


【解决方案1】:

如果我理解正确,您希望将图像返回给客户端,在这种情况下返回响应的内容,如下所示:

[HttpGet]
public async Task<FileStreamResult> GetImage()
{
    var cd = new ContentDisposition
    {
        FileName = "My awesome image.jpg",
        Inline = false  //force download instead of viewing in browser
    };

    Response.AppendHeader("Content-Disposition", cd.ToString());

    UriBuilder uriBuilder = new UriBuilder()
    {
        Scheme = "https",
        Host = "portalvhdszhm9fnx146yln.blob.core.windows.net",
        Path = "ads/logo.jpg"
    };

    using (HttpClient client = new HttpClient())
    {
        var resp = await client.GetAsync(uriBuilder.ToString());
        var content = resp.Content as StreamContent;
        var stream = await content.ReadAsStreamAsync();
        return new FileStreamResult(stream, "image/jpeg");
    }            
}

【讨论】:

    【解决方案2】:

    你可以试试:

    public class ImageController : Controller
    {
        // GET: Image
        public ActionResult Index(string containerName, string blobName)
        {
           CloudBlobContainer cont = StorageProxy.GetBlobContainer(containerName);
           CloudBlockBlob blob = cont.GetBlockBlobReference(blobName);
           MemoryStream ms = new MemoryStream();
    
           blob.DownloadToStream(ms);
           ms.Position = 0;
    
           return new FileStreamResult(ms, "image/png");
        }
    }
    

    这应该适用于来自Using Javascript to Display Blob的JS

    【讨论】:

      猜你喜欢
      • 2012-02-28
      • 1970-01-01
      • 2015-10-28
      • 2016-04-08
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多