1

How to create a text file on a client machine using javascript or jquery

Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
  • What's your final goal? What file is this? As all others have said, it's not possible so tell us the bigger picture and we'll help you paint it with different colors. – Shadow The GPT Wizard Nov 25 '10 at 08:56
  • as you have probably discovered now, varun's answer is correct. it's nutso that everyone is mindlessly voting up the "you can't do it" answer. – username May 17 '11 at 18:14
  • the fact that the wrong answer has four times as many votes as the correct one has got me mildly peeved, but i only have one vote to give. can someone else help fix the internet and spare a vote for the correct answer, please. thanks – username May 17 '11 at 21:14

2 Answers2

4

it is possible on IE and firefox... but wonder how it will work on Safari and Crome searching the same...

for FF

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
// Open the save file dialog
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);

fp.init(window, "Save File...", nsIFilePicker.modeSave);
//fp.appendFilters(nsIFilePicker.filterHTML);
fp.appendFilter("HTML File","*.htm; *.html");
fp.defaultString="data.htm";

var rv = fp.show();
if (rv == fp.returnCancel) return;

if(rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace){
    // Open the file and write to it
    var file = fp.file;
    //var filePath = file.path+".htm";
    //  //file.initWithPath(filePath);

    if(file.exists() == false){//create as necessary
        file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
    }
    var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                                          .createInstance( Components.interfaces.nsIFileOutputStream );
    outputStream.init( file, 0x04 | 0x08 | 0x20, 640, 0 );
    var result = outputStream.write( output, output.length );
    g.filename.value=file.path;
    outputStream.close();
    alert('File has been saved.' );
}

for IE

var w = window.frames.w;
if( !w ) {
    w = document.createElement( 'iframe' );
    w.id = 'w';
    w.style.display = 'none';
    document.body.insertBefore( w );
    w = window.frames.w;
    if( !w ) {
        w = window.open( '', '_temp', 'width=100,height=100' );
        if( !w ) {
            window.alert( 'Sorry, could not create file.' );
            return false;
        }
    }
}

var d = w.document;
d.open( 'text/xml', 'replace');
d.charset = "UTF-8";

d.write(JWPFormToHTML(f));
d.close();
var name= g.filename.value;

if( d.execCommand( 'SaveAs', false , name ) )
{
    g.filename.value=name;
    //document.getElementById("filename").value="";
    alert('File has been saved.' );
}
else
{
    alert( 'The file has not been saved.\nIs there a problem?' );
}
w.close();
return false;

EDIT

got it on safari and chrome as well you have to create a signed applet to create read and write files and access the local data using the same :P

Varun
  • 5,001
  • 12
  • 54
  • 85
  • msie and firefox have had this feature for ages, but please please could you share some more detail on safari. i know js, but i'm not a java guy so a link to a tutorial would be awesome :) – username May 17 '11 at 18:13
  • you have to use RHINO to interact between js and applet and then can do it easily – Varun Jun 06 '12 at 09:52
0

Not sure what your needs are exactly, but since it's not possible to write to a text file (other than cookies), could you use HTML 5 Web Storage? It's only supported in the latest browsers, see example usage here: http://davidwalsh.name/html5-storage

cspolton
  • 4,495
  • 4
  • 26
  • 34