简体   繁体   中英

How to generate a download file in javascript

I was stuck here not understood go further. some screens works same code but some not.

var element = document.createElement('a'); 
          element.setAttribute('href', 'data:text/html;charset=utf-8,' + encodeURIComponent(strOutput.replaceAll('£', '£'))); 
          element.setAttribute('download',"fpversions"); 
          element.style.display = 'none'; 
          document.body.appendChild(element); 
          element.click(); 
          document.body.removeChild(element);

can anyone help me.

this is a sample javascript code on how to download files via javascript

  var textToSave = 'this is a test';
  var hiddenElement = document.createElement('a');
  hiddenElement.href = 'data:attachment/text,' + encodeURI(textToSave);
  hiddenElement.target = '_blank';
  hiddenElement.download = 'myFile.txt';
  hiddenElement.click();

 function myFunction() { var textToSave = 'this is a test'; var hiddenElement = document.createElement('a'); hiddenElement.href = 'data:attachment/text,' + encodeURI(textToSave); hiddenElement.target = '_blank'; hiddenElement.download = 'myFile.txt'; hiddenElement.click(); }
 <button onclick="myFunction()">Click me</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM