Question

I have an application that allows users to upload photos, which are stored in Azure Blob Storage. The user also has the ability to view these photos. To view them, we want the application to download the image to the default download location. At the moment, the upload works perfect. But the download function that I found for Azure API doesn't seem to be doing anything. Also, I can't really specify a download location because this functionality needs to work on desktop/laptops as well as on mobile devices which have different default directories.

This seems like it should be simple but I cannot find anything to help me.

Here is a sample of my code:

CloudBlobContainer container = blobClient.GetContainerReference("photos");
CloudBlob blob = container.GetBlobReference(photo.BlobUrl);
//copy blob from cloud to local gallery
blob.DownloadToFile(photo.ImageName);

The blob.DownloadToFile(photo.ImageName); causes a server request but nothing happens, aka no file is downloaded.

Was it helpful?

Solution

Any reason you cannot have the client download the file directly from blob storage and use the built-in browser capability to save it? If the files are private, it is pretty easy to generate a SAS signature. This will offload the download from coming through your server and remove that overhead.

OTHER TIPS

Here:

blob.DownloadToFile(photo.ImageName);

You need to tell it where to save the file and the name you want it to be:

blob.DownloadToFile("C:\mycutepuppy.jpg");

You could use System.Environment.SpecialFolder enum to figure out the folder based on the current system then append what you want the image name to be;

Here is a more complete example:

var imgPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), + photo.ImageName);

blob.DownloadToFile(imgPath);

This would save the image to the correct path where the user's desktop directory is. Here is the documentation on that enum for special folders.

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

I wonder what do you expect from this code, but especially in the case of different platform this would not work. I suggest that you use the DownloadToStream method instead. Then you can decide what to do with the stream. For example ask the user where does he want to save the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top