1

Since the domain of the BLOB is inaccessible to some devices, I have to use my Portal(ASP.NET MVC 5) to route the request. Here is the code:

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());
        }
    }
}

My intent were to make the controller a router to a blob file. But the controller returns the following:

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
}

What I want is to show the blob image when the controller is required. What is the correct code to do it?

2
  • Have you tried returning a FileResult? Commented Sep 22, 2015 at 18:37
  • Could you please provide some sample code of returning FileResult? Commented Sep 23, 2015 at 3:25

2 Answers 2

3

If I understand you correctly, you want to return the image to the client, in that case return the content of the response like so:

[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");
    }            
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can try:

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");
    }
}

This should work with the JS from: Using Javascript to Display Blob

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.