我已经使用 WCF REST 服务创建了 Azure 云服务。我正在使用 blob 进行本地存储开发。此时,我已经能够从我的计算机将文件上传到容器中,并将它们列在 Listbox1 中。现在,我需要从 Listbox1 中选择一个项目(blob)并下载所选项目。 这是我的代码:
public void DownloadBlob(AzureDataContract download) {
// Connect to the storage account's blob endpoint
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureStorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Create the blob storage container
CloudBlobContainer container = blobClient.GetContainerReference(download.ContainerName);
container.CreateIfNotExists();
// Create the blob in the container
CloudBlockBlob blob = container.GetBlockBlobReference("test");
using (var fileStream = System.IO.File.OpenWrite(download.FilePath))
{
blob.DownloadToStream(fileStream);
}
}
然后我尝试使用 JQuery 和 Ajax 调用我的方法,如下所示:
$('#btnDownload').click(function() {
var download = {
"ContainerName": $("#listContainer").val(),
"FilePath": $("#listBlob option:selected").val()
};
$.ajax({
type: "POST",
url: "http://localhost:49416/AzureService.svc/DownloadBlob",
data: JSON.stringify(download),
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
success: function(data) {
alert("downloading");
},
error: function(data) {
alert("error");
}
});
});
最佳答案
此时您的 DownloadBlob 函数没有返回任何内容。它是一个空函数。所以你的客户端永远不会收到该 blob。它目前正在做的是将 blob 写入服务器磁盘上的文件 (download.FilePath)。如果您想在休息调用中下载任何内容,您应该将文件返回给客户端。
关于javascript - 用 JavaScript 下载 Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35670035/