1

I have managed to create and save a file with javascript as such:

<a download id=dummy_download>
  <button type="button" onclick="saveFunction()">Save</button>
</a>

<script>
function saveFunction() {
 var textToSave = "This is a long textfile generated by some other function.";
 var hiddenElement = document.getElementById('dummy_download');
 hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(textToSave);
 hiddenElement.target = '_blank';
 hiddenElement.download  = "filename.txt";
}
</script>

When I click the button "Save" the "textToSave" string will automatically be downloaded to the "Downloads" folder with the name "filename.txt". In both Chromium and Firefox it is possible to right click the "Save" button and select "Save link as..." and a dialog window will appear allowing the user to change both name and location of the file.

Is it possible to make sure we always get the dialog even when simply clicking with the left mouse button, or is the "Save link as..." simply just a web-browser feature?

I have found a ton of similar questions, even though most of them seem to boil down to the question on how to actually save something, which is not what I am asking. Some seem to indicate that it prompts a save as dialog, however, for me it just downloads the file to "Downloads".

JavaScript: Create and save file

Force a browser to save file as after clicking link

Is it possible to initiate a download prompt in the browser for recognized MIME types using only JavaScript (client-side approach)?

open save as dialog from the javascript

Heneer
  • 313
  • 6
  • 17

0 Answers0