Search code examples
javascripthtmldata-uri

Is there any way to suggest a file name with a data URI?


I am using data attribute to simulate a file download from client-side JavaScript. Here's my source code :

var data = "data:application/text,anything is here";
window.location.href = data;

This works perfectly and simulates a file download. Is there any way I can specify the file name as well, as a part of the data URI or by using some other facility available from the browser?

I am aware of the download attribute of <a> tags, but I was wondering if there are any options other than the default which is to use the value of data itself as the suggested file name.


Solution

  • I don't believe there is a way of doing it when you are redirecting the browser like that.

    If you were instead provide the download via link you could use the download attribute to suggest a filename. Not all browsers support the download attribute` at this time.