0
votes

We have thousands of files to store in Azure blob storage. These files could be of any type, but PDF files, images, and Excel are the most common. With the URI of the blob in an anchor tag, a user of our system can download a file directly from Azure to the user's computer. All is good.

We would like to have the blob name be the name of the file itself, so when the user downloads the file, the original filename is intact. However, we must account for inevitable filename collisions in the Container. Using GUIDs for the blob name is a workable solution, but when the user clicks to download the file, they will get a filename as the GUID blob name (vs. the original filename).

Is there a way to use a GUID for the blob name, but when the user clicks to download the file, the returned file has the original filename? I realize we can download the file to our server and stream a properly named file to the user, but we are trying to keep the load off our web server for file downloads.

So is there a configuration setting with a blob that would enable the file to be returned with the proper filename? We can set a MIME type for a blob, and we were looking for a similar setting for the filename part.

1

1 Answers

1
votes

Is there a way to use a GUID for the blob name, but when the user clicks to download the file, the returned file has the original filename? I realize we can download the file to our server and stream a properly named file to the user, but we are trying to keep the load off our web server for file downloads.

You can use Content-Disposition property of the blob to achieve this. What you can do is set the name of the blob as GUID however set the content-disposition property of the blob as attachment; filename="original-file-name". If you want the file to be always downloaded, you can permanently set this property. However if you want to only set this name when the file is downloaded, you can create a Shared Access Signature on the blob with Read permission and overwrite this header. I wrote about this in my blog sometime ago. From my blog post:

Assume a scenario where you want your users to download the files from your storage account but you wanted to give those files a user friendly name. Furthermore, you want your users to get prompted for saving the file instead of displaying the file in browser itself (say a PDF file opening up automatically in the browser only). To accomplish this, earlier you would need to first fetch the file from your blob storage on to your server and then write the data of that file in the response stream by setting “Content-Disposition” header. In fact, I spent a good part of last week implementing the same solution. Only if I had known that this feature is coming in storage itself:).

Now you don’t need to do that. What you could do is specify a content-disposition property on the blob and set that as “attachment; filename=yourdesiredfilename” and when your user tries to access that through a browser, they will be presented with file download option.

Now you may ask, what if I have an image file which I want to show inline also and also as a downloadable item also. Very valid requirement. Well, the smart guys in the storage team has already thought about that :). Not only you can set content-disposition as a blob property but you can override this property in a SAS URL (more on it in a bit).