How to download file from azure's blob storage

时间:2017-11-13 06:46:37

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

I am trying to download a file from azure's blob storage using jquery's $.ajax() method.

I am using the following c# code to download the blob which I believe the problem lies.

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

The above code is triggered by the following ajax call.

  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) {

                    }
                });

I have a break point on my server side c# code and it is hitting that piece of code. However the file does not download on the client end. I don't get any errors in the console either.

Any help or advice would be appreciated.

Thanks

1 个答案:

答案 0 :(得分:3)

  

我正在尝试使用jquery&#39a的$ .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的详细信息。