Search code examples
javascriptnode.jsfilesaver.js

JS FileSaver non interactive


I have an array of file to save by using a loop and i generate the name of each file. I want to save file directly (in non interactive way) without asking me to confirm. How can i do ? Here is my code for saving file

var url = img.src.replace(/^data:image\/[^;]/, 'data:application/octet-stream');

var xhr = new XMLHttpRequest();

xhr.responseType = 'blob'; //Set the response type to blob so xhr.response returns a blob
xhr.open('GET', url , true);

xhr.onreadystatechange = function () {
    if (xhr.readyState == xhr.DONE) {
        var filesaver = require('file-saver');
        filesaver.saveAs(xhr.response, nameFile); // nameFile the name of file to be saved
    }
};
xhr.send(); //Request is sent

Solution

  • Finally, i find a solution, instead of saving file, i write it by creating a new one.

    for (var i = 0; i < tabForm.length; i++) {
    
        var imgData = $('#affichageqr')[0].childNodes[1].childNodes[0].src;
    
        var data = imgData.replace(/^data:image\/\w+;base64,/, '');
    
        fs.writeFile(qrcode.getNomQRCode()+'.jpeg', data, {encoding: 'base64'}, function(err){
          if (err) {
            console.log('err', err);
          }
         console.log('success');
        });
      }