JustNoobish619
JustNoobish619

Reputation: 37

How to pass varible as the filename for a download?

Okay, I am trying to have the file be renamed whatever the user wishes to name it. However I have having trouble accomplishing this.

var a = document.createElement('a');
a.href = 'data:' + data;
a.download = 'customfilename' + '.txt';
a.innerHTML = 'DOWNLOAD FILE';

var container = document.getElementById('downloadrequestedfile');
container.appendChild(a);
   

I have tried

a.download = 'customfilename' + '.txt';
to
a.download = 'proposed_file_name' + '.txt';

to no avail and also

var customfilename = prompt('file name?');
a.download = 'customfilename' + '.txt';

I cant not figure out why its not working.

Upvotes: 0

Views: 188

Answers (1)

Andy
Andy

Reputation: 63589

Don't put the variable in quotes. For example:

const customfilename = prompt('file name?');
console.log(customfilename + '.txt');

Upvotes: 1

Related Questions