【问题标题】:How to download file from azure's blob storage如何从 azure blob 存储下载文件
【发布时间】:2018-04-25 18:59:38
【问题描述】:

我正在尝试使用 jquery 的 $.ajax() 方法从 azure 的 blob 存储下载文件。

我正在使用以下 c# 代码下载我认为问题所在的 blob。

  [System.Web.Services.WebMethod]
        public static void DownLoadBlob(string blobStorageName, string companyID)
        { 
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(System.Text.RegularExpressions.Regex.Replace(companyID.ToLower(), @"\s+", ""));
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobStorageName);
            MemoryStream memStream = new MemoryStream();
            blockBlob.DownloadToStream(memStream);
            HttpResponse response = HttpContext.Current.Response;
            response.ContentType = blockBlob.Properties.ContentType;
            response.AddHeader("Content-Disposition", "Attachment; filename=" + blobStorageName.ToString());
            response.AddHeader("Content-Length", blockBlob.Properties.Length.ToString());
            response.BinaryWrite(memStream.ToArray());
        }

上面的代码是由下面的ajax调用触发的。

  var objRecordJSON = JSON.parse(response.d);
                $.ajax({
                    type: "POST",
                    url: "FroalaImageUpload.aspx/DownLoadBlob",
                    data: '{"blobStorageName":"' + objRecordJSON[0].uploaded_file + '", ' +
                    '"companyID" : "' + $("#trainingcompanyid").val() + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {

                    },
                    failure: function (response) {

                    }
                });

我的服务器端 c# 代码上有一个断点,它正在命中那段代码。但是,该文件不会在客户端下载。我在控制台中也没有收到任何错误。

任何帮助或建议将不胜感激。

谢谢

【问题讨论】:

  • 您只是想在客户端计算机上下载文件还是想在您的JS代码中处理数据?如果是前者,那么有一个更简单的方法。你真的不需要 AJAX。
  • 是的,我只是想将文件下载到客户端计算机上。

标签: c# jquery ajax azure azure-blob-storage


【解决方案1】:

我正在尝试使用 jquery 的 $.ajax() 方法从 azure 的 blob 存储下载文件。

AFAIK,我们无法通过 Ajax 调用直接下载文件。我假设您可以创建一个 WebForm 页面并将用于输出 blob 文件的代码移动到Page_Load,并利用查询字符串传递您的参数,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    var blobStorageName = Request.QueryString["blobStorageName"];
    var companyID = Request.QueryString["companyID"];
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Microsoft.Azure.CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(System.Text.RegularExpressions.Regex.Replace(companyID.ToLower(), @"\s+", ""));
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobStorageName);
    System.IO.MemoryStream memStream = new System.IO.MemoryStream();
    blockBlob.DownloadToStream(memStream);
    HttpResponse response = HttpContext.Current.Response;
    response.ContentType = blockBlob.Properties.ContentType;
    response.AddHeader("Content-Disposition", "Attachment; filename=" + blobStorageName.ToString());
    response.AddHeader("Content-Length", blockBlob.Properties.Length.ToString());
    response.BinaryWrite(memStream.ToArray());
}

那么对于您的客户,您可以按如下方式下载文件:

window.location = "/FroalaImageUpload.aspx?blobStorageName=2017%2F11%2F7%2F2017-7-10-1.png&companyID=images";

此外,您还可以利用iframe 下载文件。详情可以参考这个类似的issue

【讨论】:

  • 您好,感谢您的回答。我已经设法通过使用具有指向我的下载功能的 onclick 事件的 <asp:Button runat="server"/> 元素来解决此问题。你是对的,我无法通过 AJAX 调用直接下载文件。
猜你喜欢
  • 1970-01-01
  • 2021-12-08
  • 2020-08-08
  • 1970-01-01
  • 2018-11-07
  • 2015-06-20
  • 2015-09-08
相关资源
最近更新 更多