0

Please someone give me a code to create a text file in hard drive

Result should be a html file, when double click html file it need to create a text file in a given path in hard drive(local).

Thank you.

2
  • 1
    Not possible. See this Commented Aug 27, 2012 at 6:50
  • You might need some workarounds as javascript is not allowed to write files to the harddisk. May be using flash or something Commented Aug 27, 2012 at 6:58

2 Answers 2

1

Javascript in a regular HTML page in a browser is not allowed direct access to a path of your choice on the hard disk for security reasons.

The, somewhat experimental, FileSystem APIs in newer browsers offering some capabilities to a sandboxed file system, but you will have to see if your need can be satisfied with those APIs.

Other than that, you would need some way around the security limitations such as doing it from a browser plug-in that the viewer has authorized and installed.

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

1 Comment

Thanks, I understand. It is not possible.
1

Something tells me that you haven't enough experience to know that a web page cannot create a file in the user's space in the position you want.

Anyway, there is a way, but you can only create a file in a sandbox, i.e. a reserved and protected space assigned by the browser.

This is possible only in the most recent browsers like Chrome... and nothing else, for now.

First, you'll have to ask for some quota:

storageInfo.requestQuota(PERSISTENT, bytes, function(quota) {
   requestFileSystem(PERSISTENT, quota, gotQuota, errorHandler);
}, function(e) {
   alert("Couldn't request quota:" + e);
});

You'll have to define two callback functions: one for success (gotQuota) and one for failure (errorHandler).

function gotQuota(fs) {
    // Creates a file
    fs.root.getFile('file.txt', {create: true}, function(fileEntry) {
       fileEntry.createWriter(function(fw) {
           fileWriter.onwriteend = function(e) {
               console.log("Write successful.");
           };
           fileWriter.onerror = function(e) {
               console.log("Write failed: " + e);
           };
           fw.write(new Blob(["This is the content"], {type: "text/plain"});
       });
    }, errorHandler);
}

Man, it's complicated... Keep in mind that some of these function are vendor prefixed (e.g. webkitStorageInfo).

Reference.

1 Comment

Thanks, I understand. It is not possible

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.