I have created an Azure Cloud Service with a WCF REST service. I am working on the local storage development with blobs. At this time, I've been able to upload files from my computer in containers and list them as well in a Listbox1. Now, i need to select an item(blob) from my Listbox1 and download the selected item. Here's my code:
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); } } Then i am trying to call my method using JQuery and Ajax as follow:
$('#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"); } }); });