Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 <a> Download Attribute in Javascript not Handling Server Error

I'm trying to programmatically click an <a> element which has the download attribute and an href to download a file from a server. However if there is an error downloading, the server will return an html page with an error status code. Because of the HTML5 download attribute it tries to force download this html page or downloads a server error file. Is there a way I can have this open the html error file in a new tab instead of downloading? I am already setting the target attribute to _blank.

like image 606
TwoHalvesFull Avatar asked Oct 20 '25 13:10

TwoHalvesFull


1 Answers

You can request the file using XMLHttpRequest or fetch, if no error occurs, set <a> element href, download attributes, else redirect user to document displaying error response.

like image 136
guest271314 Avatar answered Oct 23 '25 04:10

guest271314



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!