【发布时间】: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