Download file using javascript not working

78 Views Asked by At

i want to download array stored in filtered variable as text file but no file download while running code. If i create an array like this var data = [255,626,626,"xyz"] and use it, i am able to download text file but if i use array named "filtered" in this code, is not downloading as text file.


var x = document.querySelector("#regform > div:nth-child(8) > div > table > thead");
var nodes =x.getElementsByTagName("input");
var text = [];
for ( i = 0; i < nodes.length; i++) {
  var y =nodes[i].getAttribute("name");
  text.push(y);
};
function removeDuplicates(arr) {
  return arr.filter((item,
  index) => arr.indexOf(item) === index);
}
 var filterd = removeDuplicates(text);
var result =  Array.isArray(filterd);
console.log(result);// getting true that means this is array.
var textToBLOB = new Blob([filterd], {type: 'text/plain'});
                 
       var sFileName = 'formData.txt';     // The file to save the data.
       console.log(textToBLOB);

        var newLink = document.createElement("a");
        newLink.download = sFileName;

        if (window.webkitURL != null) {
            newLink = window.webkitURL.createObjectURL(textToBLOB);
        }
        else {
            newLink.href = window.URL.createObjectURL(textToBLOB);
            newLink.style.display = "none";
            document.body.appendChild(newLink);
        }

        newLink.click(); 

0

There are 0 best solutions below