admin管理员组文章数量:1302895
I am using Azure Blob Storage, and I want users to be able to download files. When a user clicks a file they want to download, I use AJAX to generate a shared access signature and return the URL to download from. Is there any way for me to then, using Javascript, download the file? Or is there some other method I can use... I also would like it to not run server side, as there is no reason to incur unnecessary cost when I should be able to download directly from the URL...
Basically, how Dropbox does it... Is it submitting some form somehow? It doesn't even have to be like that.
I am using Azure Blob Storage, and I want users to be able to download files. When a user clicks a file they want to download, I use AJAX to generate a shared access signature and return the URL to download from. Is there any way for me to then, using Javascript, download the file? Or is there some other method I can use... I also would like it to not run server side, as there is no reason to incur unnecessary cost when I should be able to download directly from the URL...
Basically, how Dropbox does it... Is it submitting some form somehow? It doesn't even have to be like that.
Share Improve this question asked Dec 16, 2013 at 23:34 PaulPaul 3851 gold badge5 silver badges17 bronze badges3 Answers
Reset to default 4If I understand correctly, you want your users to see Open/Save As
prompt when they click on the SAS URL.
Assuming my understanding is correct, recently Windows Azure Storage had announced some enhancements. One of the enhancement there is addition of Content-Disposition
property for the blob which would allow this Open/Save As
prompt to show when a user clicks on the SAS link for the blob. With Content-Disposition
, you have 2 options:
- Set
Content-Disposition
as blob property: In this case, whenever user clicks on the SAS link, they will be prompted to save the file. But in this case, they will always be prompted so SAS link or not, user would never be able to see the document inline in the browser. You may find this link useful for this: http://msdn.microsoft./en-us/library/windowsazure/ee691966.aspx. - Set
Content-Disposition
as part of SAS URL: In this case, a user will only be prompted when they click on the SAS URL which containsContent-Disposition
. You may find this link useful for this: http://msdn.microsoft./en-us/library/windowsazure/dn140255.aspx.
So assuming you have a png file. In option 1, it will never be displayed inline in the browser, it will be downloaded on the client machine and opened in the registered image viewer on your puter. In option 2, if Content-Disposition
is set in the SAS URL, the image file will be downloaded on the client machine and opened in the registered image viewer on your puter otherwise it will be displayed directly in the browser.
If you're using .Net, Storage Client library 3.0.0.0 has support for this feature so you don't have to write wrapper around the REST API (but I think you're using PHP ... from your previous question about CORS :)).
The way that I've done this in the past is by opening a hidden iframe with the url for the download.
Something along the lines of
var frame = document.createElement('iframe');
frame.src = url; // your Azure url
frame.style.display = 'none';
document.body.appendChild(frame);
Note that you'll need to make sure that you don't try to do this on a secure (https) page with an insecure download (http)
To avoid reload/redirection, I use this library :
http://jqueryfiledownload.apphb./
If you need help to set it up : http://johnculviner./jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
But if you want to redirect the user, you could use basic thing like :
window.location.href = "http://stackoverflow./file.pdf";
本文标签: Javascript download a URLAzure Blob StorageStack Overflow
版权声明:本文标题:Javascript download a URL - Azure Blob Storage - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741709512a2393758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论