如何使用下载链接从Azure Blob存储下载文件 [英] How to download files from Azure Blob Storage with a Download Link

查看:1040
本文介绍了如何使用下载链接从Azure Blob存储下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提供了Azure云服务,您可以在其中使用Blob将文件上传和删除到云存储中.我成功编写了一种方法,您可以从云服务中删除上传的Blob:

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

这也是使用HTML进行查看的代码:

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> 

现在,我想编写一个类似的方法,以便您可以从视图"中下载每个Blob.我试图使用与删除操作完全相同的代码来编写methode,而不是

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

现在

blob.DownloadToFile(File);

这没有用.是否有可能更改delete方法,以便它下载选定的Blob而不是将其删除?

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

这是DownloadToFile方法的代码:

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.

我得到的异常是:

UnauthorizedAccessException:对路径"C:\ Program Files \ IIS Express \ Eva Passwort.docx"的访问被拒绝.]

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?

推荐答案

我想编写一个类似的方法,以便您可以从视图中下载每个Blob.

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

您似乎希望允许用户下载blob文件,以下示例代码对我而言效果很好,请参考.

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

注意:有关 Controller.File方法.

这篇关于如何使用下载链接从Azure Blob存储下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆