Downloading a file using JavaScript

This is a way to download a file by utilizing the DOM.

In this scenario, csv is a string variable with all the values to be in the table separated by commas. We specify the URI as csv and aptly name the file “myfile.csv”.

encodeURI is used on the variable csv to enforce escape sequence representation of UTF-8 encoding to the characters of the variable csv.

var uri = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
var link = document.createElement("a");
$(link).hide();
link.href = uri;
link.download = "myfile.csv";
$("body").append(link);
link.click();
$("body").remove(link);
 
10
Kudos
 
10
Kudos

Now read this

ES6 Array.prototype.fill

So recently I tried to initialize an array of empty objects in javascript. I wanted it to be configurable with how many elements are present in the initial array as well as what I initialize it with (it may not be an empty object in the... Continue →