FileSaver.js icon indicating copy to clipboard operation
FileSaver.js copied to clipboard

cors detection not working in MS Edge

Open th3coop opened this issue 4 years ago • 2 comments

Using FileSaver.js 2.0.4.

I think this is going to be an "Edge" case but I'm having an issue where download("...") fails in MS Edge with a CORS error before it even gets into saveAs().

From the browser console:

download("[...REDACTED...]")
Access to XMLHttpRequest at '[...REDACTED...]' from origin 'http://suro.test' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
FileSaver.js:42 could not download file
xhr.onerror @ FileSaver.js:42
error (async)
download @ FileSaver.js:41
(anonymous) @ VM3573:1
FileSaver.js:44 GET [...REDACTED...] net::ERR_FAILED

I believe I traced the issue to what I'm fairly certain is a bug in Edge but it struck me that FileSaver performs a CORS check and has alternative methods of downloading a file if it fails so this ideally shouldn't have failed.

It seems like you could perform the CORS check sooner and pass url to saveAs instead of the xhr.response if the CORS fails.

Am I way off? Should I just go to bed? Thanks for listening! I'm sure I could find some time to make a PR tomorrow or this weekend if the above sounds like it makes sense.

th3coop avatar May 14 '21 07:05 th3coop

OH, I don't think my idea will work either because Edge appears to not properly respect a[download] (which is how I originally came to FileSaver.js) so if you do FileSaver.saveAs("https://httpbin.org/image", "image.jpg"); in Edge it just opens the file in a new tab 🤦 (🤦 is for Edge, not FileSaver).

th3coop avatar May 14 '21 07:05 th3coop

I'm having the same problem with Chrome and mentioned it here But the thing is: second click will work, seem like the browser will trust the link and allow you to download after first attemp https://github.com/eligrey/FileSaver.js/issues/650#issuecomment-1291448422

imrim12 avatar Oct 26 '22 03:10 imrim12