1

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

3
  • 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. Commented Nov 25, 2010 at 8: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. Commented May 17, 2011 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 Commented May 17, 2011 at 21:14

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 :)
you have to use RHINO to interact between js and applet and then can do it easily
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.