admin管理员组

文章数量:1290964

I wish to create a text file locally on my system using javascript/jquery.

I am trying this code, but not working on my system.

Machine : Ubuntu 10.4 Chrome : 14.0.835.126

window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
        fs.root.getFile('~/Desktop/test.txt', {create: true}, function(fileEntry) {
            alert(fileEntry.fullPath);   //getting filepath
        }, function() {});
    }, function() {});

I wish to create a text file locally on my system using javascript/jquery.

I am trying this code, but not working on my system.

Machine : Ubuntu 10.4 Chrome : 14.0.835.126

window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
        fs.root.getFile('~/Desktop/test.txt', {create: true}, function(fileEntry) {
            alert(fileEntry.fullPath);   //getting filepath
        }, function() {});
    }, function() {});
Share Improve this question edited Oct 7, 2011 at 9:09 Saurabh Saxena asked Oct 7, 2011 at 8:58 Saurabh SaxenaSaurabh Saxena 3,21511 gold badges35 silver badges46 bronze badges 9
  • Proof me wrong, but i do not think, that this is possible. You can set a cookie for sure. – madc Commented Oct 7, 2011 at 9:06
  • 2 What do you mean by "not working"? Are you seeing errors? Did you write this code from scratch or copy it from a certain tutorial or reference site, and what is the URL? We need more information. – Jordan Running Commented Oct 7, 2011 at 9:17
  • ChecK This Fiddle This works only for .bin files. – Saurabh Saxena Commented Oct 7, 2011 at 9:20
  • I am not getting any errors. This code is working fine on jsFiddle – Saurabh Saxena Commented Oct 7, 2011 at 9:21
  • If the code is working fine then what is your question? – nnnnnn Commented Oct 7, 2011 at 10:39
 |  Show 4 more ments

1 Answer 1

Reset to default 6

This is a little tricky but working

chrome.browserAction.onClicked.addListener(createFile);
createFile();

function createFile()
{
    chrome.tabs.getSelected(null, function(tab) {
        window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
            fs.root.getFile('test', {create: true}, function(fileEntry) {
                fileEntry.createWriter(function(fileWriter) {
                    var builder = new WebKitBlobBuilder();
                    builder.append("Saurabh");
                    builder.append("\n");
                    builder.append("Saxena");

                    var blob = builder.getBlob('text/plain');

                    fileWriter.onwriteend = function() {
                        chrome.tabs.create({"url":fileEntry.toURL(),"selected":true},function(tab){});
                    };
                    fileWriter.write(blob);
                }, errorHandler);
            }, errorHandler);
        }, errorHandler);
    });
}
function errorHandler(e) {
  var msg = '';

  switch (e.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;
  };

  Console.Log('Error: ' + msg);
}

Because of the Security Exceptions, i cannot create/modify a file on Local System. But in this code, I am actually creating a file in a directory which is allocated for Google Chrome Temporary Files and then downloading that file into my Downloads Folder.

This is the code of the popup page of a Chrome Extension.

:)

本文标签: htmlHow to Create a Text File Locally at client side using JavaScriptJQueryStack Overflow