3

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.

1

2 Answers 2

7

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>

0
0

in my case using the idea of robin i put the next code for a url in base64 let url1 = data:application/pdf;base64, ${response.data.c_planilla[0].PLANILLA} var hiddenElement = document.createElement('a'); hiddenElement.href = url1; hiddenElement.target = '_blank'; hiddenElement.download = 'Planilla ARI.pdf'; hiddenElement.click();

1
  • What is the different between your post and the one you are mentionning?
    – Elikill58
    Commented Feb 26 at 15:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.