Calling a user to save a file using the Save-As dialog box?

I currently have this code:

function download(filename, text) { var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); pom.setAttribute('download', filename); pom.click(); } download('test.html', string); 

string contains a lot of HTML code that is written to the HTML file.
The above code works fine: when you click the button, the browser (chrome) automatically loads the HTML file with the contents of the string written to it.

Now, instead of automatically downloading the chrome file, I want to open the "save as" dialog box and ask the user for the location and name of the file, and then upload it to this place.

A quick simple answer would be greatly appreciated.

+6
source share
2 answers

My browser was configured to automatically download all files by default, so not only this file was downloaded not only by this file, but also all other files from my browser that were downloaded directly without a save invitation dialog. A change in browser settings has worked to "always ask for the download location."

+4
source

The only way to do this is to set the file header on the server, for example:

 <FilesMatch "\.(?i:pdf)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch> 

The download attribute does not allow you to change the file name or file type anymore, as this is an obvious security risk.

What you are trying to do is replicate the dialog with the right mouse button - save-as, but I'm afraid that this is currently not possible.

+2
source

Source: https://habr.com/ru/post/957021/


All Articles