i want to create a text file from a string. currently i am using a function which takes an array and makes it into a string then using that string i want to create a local text file the user downloads. i have tried using this method
function createFile(){ //creates a file using the fileLIST list
var output= 'Name \t Status\n'+ fileLIST[0][0].name+'\t'+fileLIST[0][1]+'\n';
var Previous = fileLIST[0];
for (var i=1; i<fileLIST.length; i++)
if (fileLIST[i][1] =='none' || fileLIST[i][1] == Previous[1])
continue
else {
Previous = fileLIST[i]
output = output + fileLIST[i][0].name +'\t'+fileLIST[i][1] + '\n';}
window.open("data:text/json;charset=utf-8," + escape(output));//should create file
display(); }
i am using chrome as my browser. also i would prefer JS or HTML5 answer.
thank you in advance
i ended up using this code instead. it creates a link to download the url of the file.
also this way adds less to the website making it a lighter website for slow connections. my html has a empty div in which this appends to.
BlobBuilder is now obsolete. Use this instead:
https://developer.mozilla.org/en-US/docs/Web/API/Blob#Blob_constructor_example_usage
Convert your
object
to a JSON string.Notes:
, undefined, 2
.Create a download link and click it:
I suggest you use a hidden
iframe
instead ofwindow.open
to make it "a cleaner method"and I believe it's
text/octet-stream
and nottext/json
to force the download. And as far as I know, you can't set the file name this way.However, Chrome (18+ I think) has a
download
attribute for its<a>
tags which you can specify the name of the file along with usingblob:
for the url.