Sunday, May 19, 2024
142
rated 0 times [  143] [ 1]  / answers: 1 / hits: 38264  / 11 Years ago, fri, january 17, 2014, 12:00:00

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



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


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 window.location.href ?


More From » export-to-csv

 Answers
59
function downloadFile(fileName, urlData) {

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

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


http://jsfiddle.net/rooseve/7bUG8/


[#73112] Thursday, January 16, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;