简体   繁体   中英

Downloading files from javascript

I have a .NET web application that houses a large number of downloadable documents. I want to move these files offsite to Azure storage. However, given a particular file's URL, I need a way to download it from the client browser (using Javascript of course). The behavior I am seeking is for the browser's 'Save File As' functionality to invoke allowing the user to save the file to their local computer.

I have the following code attempt that isn't working. I am using a script tag b/c that is the only way I can get around the cross domain scripting issue. However, when looking with Fiddler, the request is never even attempted. It's my understanding that setting the .src property should invoke the download.

var script = document.createElement('script');
script.type = 'application/pdf';
script.src = 'http://myazureaccount.com/myPDFfile.pdf';

Can someone show me a way to accomplish this? There might not be a way to do it. Also I don't want to run any sort of server side code in Azure. I just want to use it as an external file store.

You should use a Custom Domain for your Azure Storage account so it comes from the same domain ( eg files.mydomain.com ). This will solve the cross-site scripting (XSS) issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM