coffeemonitor coffeemonitor - 1 year ago 282
Javascript Question

Javascript - Download CSV as File

I'm messing with some javascript to download some csv text:

var data = '"Column One","Column Two","Column Three"';
window.location.href = 'data:text/csv;charset=UTF-8,' + encodeURIComponent(data);

So far this is working, but as the browser prompts me to save the file, there is no filename, nor extension.

How can I predetermine the name of the file and it's extension, inside the

Answer Source
function downloadFile(fileName, urlData) {

    var aLink = document.createElement('a');
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent("click"); = fileName;
    aLink.href = urlData;

var data = '"Column One","Column Two","Column Three"';
downloadFile('2.csv', 'data:text/csv;charset=UTF-8,' + encodeURIComponent(data));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download