Can I generate a file in javascript and request a user download?

Is it possible to create a file in javascript and then request the user to load?

We would like to do the following: we want to create a small html form where the user can edit the values ​​in the table, and we would like to be able to generate a csv file from the form and request to save it on our hard drive. Is this possible in pure javascript? If not, are there any workarounds?

Note. I know that I could solve this by contacting the server, submitting the form and receiving the csv file, but the trick is that there is no server, the html form will simply be generated on the hard drive.

+5
source share
3 answers

, .

csv , .

+1

flash, Downloadify. downloadify.swf, javascript.

. -:

<script type="text/javascript">
  Downloadify.create('downloadify',{
    filename: function(){
      return document.getElementById('filename').value;
    },
    data: function(){ 
      return document.getElementById('data').value;
    },
    onComplete: function(){ 
      alert('Your File Has Been Saved!'); 
    },
    onCancel: function(){ 
      alert('You have cancelled the saving of this file.');
    },
    onError: function(){ 
      alert('You must put something in the File Contents or there will be nothing to save!'); 
    },
    transparent: false,
    swf: 'media/downloadify.swf',
    downloadImage: 'images/download.png',
    width: 100,
    height: 30,
    transparent: true,
    append: false
  });
</script>
+1

javascript .

script, / , .

Fungus javascript, , , ( > ... ..).

,

0
source

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


All Articles