How to download files from Azure Blob Storage with a Download Link

你说的曾经没有我的故事 提交于 2019-12-12 20:42:33

问题


I made an Azure Cloud Service, where you can upload and delete files to the cloud storage using Blobs. I wrote sucessfully a method where you can delete the uploaded blobs from the cloud service:

 public string DeleteImage(string Name)
    {
        Uri uri = new Uri(Name);
        string filename = System.IO.Path.GetFileName(uri.LocalPath);

        CloudBlobContainer blobContainer = _blobStorageService.GetCloudBlobContainer();
        CloudBlockBlob blob = blobContainer.GetBlockBlobReference(filename);

        blob.Delete();

        return "File Deleted";
    }
}

Here is also the code for View with HTML:

@{
ViewBag.Title = "Upload";
}

<h2>Upload Image</h2>

<p>
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = 
"multipart/form-data" }))
{
    <input type="file" name="image"/>
    <input type="submit" value="upload" />
}

</p>

<ul style="list-style-position:Outside;padding:0;">
@foreach (var item in Model)
{
<li>
    <img src="@item" alt="image here" width="100" height="100" />
    <a id="@item" href="#" onclick="deleteImage ('@item');">Delete</a>

</li>
}
</ul>

<script>
function deleteImage(item) {
    var url = "/Home/DeleteImage";
    $.post(url, { Name: item }, function (data){
        window.location.href = "/Home/Upload";
    });
}

</script> 

Now I want to write a similiar method so you can download each blob from the View. I tried to write the methode using exactly the same code from the delete but instead of

blob.delete();

now

blob.DownloadToFile(File);

This didn't work though. Is there a possibility to change the delete method so it downloads the chosen blob instead of deleting it?


Added Info

Here is the code of DownloadToFile method:

[HttpPost]
    public string DownloadImage(string Name)
    {
        Uri uri = new Uri(Name);
        string filename = System.IO.Path.GetFileName(uri.LocalPath);

        CloudBlobContainer blobContainer = 
_blobStorageService.GetCloudBlobContainer();
        CloudBlockBlob blob = blobContainer.GetBlockBlobReference(filename);

        blob.DownloadToFile(filename, System.IO.FileMode.Create);


        return "File Downloaded";
    }

The name is just the whole filename that is uploaded. Filename is the data path.

The Exception I get is:

UnauthorizedAccessException: The access to the path "C:\Program Files\IIS Express\Eva Passwort.docx" is denied.]

I think that the problem is that my application doesn't have a path to save the file. Is there a possibility to get a dialog where I can chose the path to save?


回答1:


I want to write a similiar method so you can download each blob from the View.

It seems that you'd like to enable users to download the blob files, the following sample code work fine on my side, please refer to it.

public ActionResult DownloadImage()
{
    try
    {
        var filename = "xxx.PNG";
        var storageAccount = CloudStorageAccount.Parse("{connection_string}");
        var blobClient = storageAccount.CreateCloudBlobClient();

        CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
        CloudBlockBlob blob = container.GetBlockBlobReference(filename);

        Stream blobStream = blob.OpenRead();

        return File(blobStream, blob.Properties.ContentType, filename);

    }
    catch (Exception)
    {
        //download failed 
        //handle exception
        throw;
    }
}

Note: Detailed information about Controller.File Method.



来源:https://stackoverflow.com/questions/45442818/how-to-download-files-from-azure-blob-storage-with-a-download-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!