Create Text file from String using JS and html5
Asked Answered
G

3

22

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

Gathard answered 18/5, 2012 at 14:57 Comment(4)
and what's wrong with this code?Plasty
it opens a new tab and doesn't do anything. it is not creating a file to downloadGathard
Is the code in the question server-side JS?Ostrich
no it is all local. i have include the --allow-file-access-from-files in chrome to see if that would help but no.Gathard
G
4

i ended up using this code instead. it creates a link to download the url of the file.

     window.URL = window.webkitURL || window.URL;
    window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder ||       window.MozBlobBuilder;
    file = new WebKitBlobBuilder();
    file.append(output); 
    var a = document.getElementById("downloadFile");
    a.hidden = '';
    a.href = window.URL.createObjectURL(file.getBlob('text/plain'));
    a.download = 'filename.txt';
    a.textContent = 'Download 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.

   <div class ='paginationLIST' id='pagination'></div>
Gathard answered 22/5, 2012 at 18:38 Comment(1)
maybe i miss something, but the code souldn't be something like: file = new BlobBuilder because you have assigned to this object the Webkit or Moz BlodBuilder.. Am I right?Dentistry
C
45

Convert your object to a JSON string.

var json_string = JSON.stringify(object, undefined, 2);

Notes:

  1. If you already have a string, skip the step above.
  2. If you don't want it to be formatted nicely, remove the , undefined, 2.

Create a download link and click it:

var link = document.createElement('a');
link.download = 'data.json';
var blob = new Blob([json_string], {type: 'text/plain'});
link.href = window.URL.createObjectURL(blob);
link.click();
Commons answered 10/2, 2015 at 1:42 Comment(1)
Thank you so much! Works like a charm :-)Ardeliaardelis
R
5

BlobBuilder is now obsolete. Use this instead:

https://developer.mozilla.org/en-US/docs/Web/API/Blob#Blob_constructor_example_usage

Ruralize answered 21/6, 2013 at 20:0 Comment(0)
G
4

i ended up using this code instead. it creates a link to download the url of the file.

     window.URL = window.webkitURL || window.URL;
    window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder ||       window.MozBlobBuilder;
    file = new WebKitBlobBuilder();
    file.append(output); 
    var a = document.getElementById("downloadFile");
    a.hidden = '';
    a.href = window.URL.createObjectURL(file.getBlob('text/plain'));
    a.download = 'filename.txt';
    a.textContent = 'Download 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.

   <div class ='paginationLIST' id='pagination'></div>
Gathard answered 22/5, 2012 at 18:38 Comment(1)
maybe i miss something, but the code souldn't be something like: file = new BlobBuilder because you have assigned to this object the Webkit or Moz BlodBuilder.. Am I right?Dentistry

© 2022 - 2024 — McMap. All rights reserved.