none
How to download File from azure storage (blob) RRS feed

  • Question

  • public class BlobStorageRepository : IBlobStorageRepository
        {
            private StorageCredentials _storageCredentialsx;
            private CloudStorageAccount _cloudStorageAccountx;
            private CloudBlobContainer _cloudBlobContainerx;
            private CloudBlobClient _cloudBlobClientx;

            private string containerNamex = "mycontainer";
            private string downloadPath = @"D:\Images\";


            public BlobStorageRepository()
            {
                string accountName = "hachim";
                string keyx = "IL5iWGg+pNHKQ3IZC5D8V4xeummZwkfmbyA2GAdJF3ACT+piQxLRZqjZOXOCpJelGaDgyjSBgnZFigo4pxj/OA==";

                    _storageCredentialsx = new StorageCredentials(accountName, keyx); //set the azure storage credentals 
                _cloudStorageAccountx = new CloudStorageAccount(_storageCredentialsx, true); //connect to storage service 
                _cloudBlobClientx = _cloudStorageAccountx.CreateCloudBlobClient(); //create the blob service client
                _cloudBlobContainerx = _cloudBlobClientx.GetContainerReference(containerNamex); //contains all blobs for container


            }

    and this is my download async class:

     public async Task<string> DownloadBlobAsync (string file, string fileExtension)
            {
                _cloudBlobContainerx = _cloudBlobClientx.GetContainerReference(containerNamex);
                CloudBlockBlob blockBlob = _cloudBlobContainerx.GetBlockBlobReference(file + "." + fileExtension);

                var path = downloadPath + file + "." + fileExtension;
                using (var fileStream = System.IO.File.OpenWrite(path))
                {
                    //fileStream.Position = 1;
                    //fileStream.Seek(0, SeekOrigin.Begin);

                    await blockBlob.DownloadToStreamAsync(fileStream);

                    return path;
                }
            }

    and my download class from controller:

            public async Task<ActionResult> DownloadBlob(string file, string extension)
            {
                string downloadPath = await repo.DownloadBlobAsync(file, extension);
                return Json(downloadPath);
            }

    my razor:

    @model IEnumerable<WebRole1.Models.BlobViewModel>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <div class="container">

        @Html.ActionLink("Upload to Azure Blob", "UploadBlob", new { Controller = "blob" }, new { @class = "btn btn-link" })

        <div class=" table table-striped table-responsive">

            <table id=" tablex">
                <thead>
                    <tr>
                        <th>Container</th>
                        <th>Actual FileName</th>
                        <th> Uri </th>

                    </tr>
                </thead>

                <tbody>
                    @if(Model != null)
                    {
                        foreach (var item in Model)
                        {

                    <tr id="row_@item.PrimaryUri">
                        <td>@item.BlockContainerName</td>
                        <td>@item.ActualFileName</td>

                        <td>
                            <a herf=@item.PrimaryUri>@item.PrimaryUri </a>
                        </td>
                        @*<td>@Html.ActionLink("Remove", "DeletedBlob", new { controller = "blob", file = @item.FileNameWithoutExt, extension = @item.FileNameWithoutExt })</td> *@

                        <td>@Html.ActionLink("Download", "DownloadBlob", new { controller = "blob", file = @item.FileNameWithoutExt, extension = @item.ActualFileName })</td>
                       
                        <td>
                            <input type="submit" href="#" class="btn btn-link" id="btndel" value="Remove" data-id="@item.ActualFileName" />
                        </td>

                    </tr>

                        }

                    }

                </tbody>



            </table>
        </div>
    </div>

    However for some reason when i click my download link i get a error saying "this file format cannot be opened" is it because it isnt getting the path required?

    image downloads to file however cannot open it and it has 0bytes everytime downloaded 

         
    • Edited by Goten2022 Tuesday, March 27, 2018 9:00 PM
    • Moved by Fei Hu Thursday, March 29, 2018 7:11 AM asp.
    Tuesday, March 27, 2018 8:58 PM

All replies

  • Hello Goten2022,

    You question is more related to Asp.Net, you need to repost a new thread to Azure and ASP.NET forum for better support and solution.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 29, 2018 7:11 AM