locked
Downloading data from Blob storage RRS feed

  • Question

  • Hi,,,

          I have uploaded data in my blob storage. Now  I want to download a data when i gives the corresponding blobAddressUri. I use the following code in the click event of download button.

                MemoryStream mem = new MemoryStream();
                mem = (MemoryStream)Blob.GetBlob(blobAddressUri);
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=<filename>");
                Response.AddHeader("Content-Length", mem.Length.ToString());
                Response.ContentType = "application/octet-stream";
                byte[] cc = new byte[mem.Length];
                cc = mem.ToArray();
                Response.Write(cc);
                 Response.Flush();

     

    But this does work...Eg: If i attached a doc file  then While clicking download button I gets only empty doc file... How can I implemet this???????? I have to store very heavy file in blob storage(say in GBs). 

    Thanks in advance...        

    Wednesday, April 13, 2011 12:26 PM

Answers

  • Hello Bon,

    I understand that you want to download a file from blob stroage to the browser.

    It is not recommended to completely download all data bytes into a binary array as it will consume much memory on server. Instead, we can set the blob container to allow public access. The code snippet will be:

    var permissions = container.GetPermissions();
    permissions.PublicAccess = BlobContainerPublicAccessType.Container;
    container.SetPermissions(permissions);

    The container is the container of your blobs. It is a CloudBlobContainer object.

    After that, if you want to download a file from blob storage to browser, you can directlly redirect the user to the blob uri. And the browser will download the particular file from the specific uri.

    Response.Redirect(blob.Uri.ToString());

    blob variable is a CloudBlockBlob object.

    Thanks,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework


    • Marked as answer by Wenchao Zeng Wednesday, April 20, 2011 3:01 AM
    Friday, April 15, 2011 9:01 AM

All replies

  • Hi BonDavi, I'm wondering why can't you just return the blob.Uri.ToString() to the client?  Also could you make sure what you uploaded is not empty, I had this issue I posted here
    Wednesday, April 13, 2011 3:19 PM
  • Hiii,,,,

    I used "

                      "" myArray = (byte[])  Blob.GetBlobReference(blobAddress).DownloadByteArray();

                         Response.BinaryWrite(array);
                            Response.Flush();""

    and adds "<httpRuntime maxRequestLength="1048576" executionTimeout="600" />" to my web config and i could download data upto 1 Gb size... but i could not retrive content type of my document .. I upload data using code  

    CloudBlob blobRef = Blob.GetBlobReference(fileName);
                blobRef.Properties.ContentType = contentType;
                blobRef.UploadFromStream(stream);

     

    and when i tried to retrive content type of the data , it is displaying null...  I used " string contentType = Blob.GetBlobReference(blobAddress).Properties.ContentType; " to retrive content type...Please help

     Thanks in advance

     

              "

    Thursday, April 14, 2011 11:32 AM
  • Hello Bon,

    I understand that you want to download a file from blob stroage to the browser.

    It is not recommended to completely download all data bytes into a binary array as it will consume much memory on server. Instead, we can set the blob container to allow public access. The code snippet will be:

    var permissions = container.GetPermissions();
    permissions.PublicAccess = BlobContainerPublicAccessType.Container;
    container.SetPermissions(permissions);

    The container is the container of your blobs. It is a CloudBlobContainer object.

    After that, if you want to download a file from blob storage to browser, you can directlly redirect the user to the blob uri. And the browser will download the particular file from the specific uri.

    Response.Redirect(blob.Uri.ToString());

    blob variable is a CloudBlockBlob object.

    Thanks,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework


    • Marked as answer by Wenchao Zeng Wednesday, April 20, 2011 3:01 AM
    Friday, April 15, 2011 9:01 AM