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 Object Enhancements

This is the part of a series of blogs on the new features on the upcoming ECMAScript 6 (ES6) specification which JavaScript implements. In this blog we focus on the object literal property initialization enhancements introduced in ES6.... Continue →