Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change file name when using window.location to download

I'm using window.location to download my image. It isn't in HTML because I generate the image on the server and then send it back down so it looks like :

window.location = data.url;

I've seen a few other questions but they suggest the download attr which I don't have because there's no HTML.

Is there a way I can change the file name?

like image 985
pourmesomecode Avatar asked Mar 13 '23 18:03

pourmesomecode


1 Answers

Front-end solution

The only thing you can do on the front-end side is to change your code to HTML <a> element with download attribute:

<a href="my_file.pdf" download="very_important_report.pdf">Download</a>

When user clicks this link, the browser forces download and saves the file with given filename. You can read more about it in this post. It's quite a new feature so check the browser support.

Back-end solution

If you can modify the server-side code then you should use content-disposition header as defined in RFC 2183.

content-disposition: attachment; filename=very_important_report.pdf
like image 197
Michał Miszczyszyn Avatar answered Mar 16 '23 02:03

Michał Miszczyszyn