C# 如何从azure下载文件';s blob存储
我正在尝试使用jquery的$.ajax()方法从azure的blob存储下载一个文件 我正在使用下面的c代码下载blob,我认为问题出在这里C# 如何从azure下载文件';s blob存储,c#,jquery,ajax,azure,azure-storage-blobs,C#,Jquery,Ajax,Azure,Azure Storage Blobs,我正在尝试使用jquery的$.ajax()方法从azure的blob存储下载一个文件 我正在使用下面的c代码下载blob,我认为问题出在这里 [System.Web.Services.WebMethod] public static void DownLoadBlob(string blobStorageName, string companyID) { CloudStorageAccount storageAccount = Cl
[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#代码有一个断点,它正在命中这段代码。但是,该文件不会在客户端下载。我也没有在控制台中得到任何错误
任何帮助或建议都将不胜感激
谢谢
我正在尝试使用jquery的$.ajax()方法从azure的blob存储下载一个文件
抱歉,我们无法通过Ajax调用直接下载文件。我假设您可以创建一个WebForm页面,并将用于输出blob文件的代码移动到页面加载
,并利用querystring传递参数,如下所示:
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";
然后,对于您的客户端,您可以按如下方式下载该文件:
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
下载文件。您可以参考类似的详细信息。您只是想在客户端计算机上下载文件,还是想在JS代码中处理数据?如果是前者,那么有一个更简单的方法。你真的不需要AJAX。是的,我只是想在客户端计算机上下载文件。嗨,谢谢你的回答。我通过使用一个
元素解决了这个问题,该元素有一个指向我的下载函数的onclick事件。你是对的,我不能通过AJAX调用直接下载文件。