How to download files from Azure Blob Storage with

2019-07-15 01:13发布

问题:

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.