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

Javascript Memory Leaks

Memory leaks are often associated with big enterprise RESTful services when an object is stored in memory but cannot be accessed by any of the running code. This causes diminishing performance ability of the application by reducing the... Continue →