简体   繁体   中英

jQuery / javascript download file doesn't work on Chrome

I have a jQuery script in which I want to download a file which is located on some server after the user clicks on the link. On IE11 this works alright, as I simply make a call to:

window.open('file:///MyServer0001/SomeFolder/Tmp/ToiletFlush.log'

I know that for security reasons, Chrome doesn't allow to use the protocol file:/// . It is interesting because if I write myself this URL on the address bar he will show the file, however if I make a call for window.open() he just opens me a new empty window. Also, removing the file:/// , doesn't do anything on Chrome, it says that the page is not available

So right now I don't know how to make this work on chromium. I thought I could make a workaround by calling the controller function through a POST/GET , return a FileResult and download the file. This definitely works but I would like to know a simple way to do this directly only using jQuery/JavaScript.

Any Idea?

Try ASP.NET MVC FileResult:

View:

<a href='@Url.Action('GetFile','Home')">Download File</a>

In Home Controller:

public FileResult GetFile()
{
//Read file content into variable.
string content=File.ReadAllText("filepath");
byte[] byteArray = Encoding.ASCII.GetBytes(content);
return File(byteArray, System.Net.Mime.MediaTypeNames.Text.Plain, "ToiletFlush.log");
}

Try this,

On Windows:
1. Find the local URL of your Chrome Install
2. From the Command Line launch Chrome w/ '–allow-file-access-from-files' prepended to it:

–allow-file-access-from-files C://...

On OSX, just type this in Terminal:

open /Applications/Google\ Chrome.app --args --allow-file-access-from-files

Using Google Chrome Canary is also possible:

open /Applications/Google\ Chrome\ Canary.app --args --allow-file-access-from-files

Or... You'll need to run your application on a local server.

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