In my DownloadFile
page's Page_Load
method, the filename a user wishes to download is retrieved from the query string. The file is hosted in an Azure Blob Storage account. I am attempting to download the file using shared access signatures (SAS) via this approach:
var containerName = "containerName";
var con = "connectionString";
CloudStorageAccount account = CloudStorageAccount.Parse(con);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
var blob = container.GetBlockBlobReference("file.pdf");
var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),
}, new SharedAccessBlobHeaders()
{
ContentDisposition = "attachment; filename=file.pdf"
});
var blobUrl = string.Format("{0}{1}", blob.Uri.AbsoluteUri, sasToken);
Response.Redirect(blobUrl);
However, this does not download the file. Instead, the browser just shows a garbled character stream of the file:
The blobUrl
appears to be valid. But the Response.Redirect
isn't working as expected. Am I doing something wrong?
Note: I am using WebForms (unfortunately), not MVC
I was able to figure out a workaround by using an iframe
. If I add an iframe
to the page:
<iframe id="iframeFile" runat="server" style="display:none;"></iframe>
Then set the source in the codefile:
iframeFile.Src = blobUrl;
It downloads the file.
This may not be ideal, and I'm still not sure why Response.Redirect
isn't working as expected. So I'm certainly open to other suggestions as well. But this workaround does resolve the issue of not being able to download the file.