最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML5 fileWriter.write doesn't write to local file - Stack Overflow

programmeradmin0浏览0评论

I am working on the tutorial for HTML5 FileSystem-API. Basically I would like to write a file to my machine. I am using python's simpleHTTPServer to serve the website. Once I load the webpage, the console doesn't raise an error and I get a message that the write was successful. But I can't find the file anywhere on my machine. Here is my code:

function onInitFs(fs) {

  fs.root.getFile('zz11zzlog.txt', {create: true}, function(fileEntry) {

    // Create a FileWriter object for our FileEntry (log.txt).
    fileEntry.createWriter(function(fileWriter) {

      fileWriter.onwriteend = function(e) {
        console.log('Write pleted.');
      };

      fileWriter.onerror = function(e) {
        console.log('Write failed: ' + e.toString());
      };

      // Create a new Blob and write it to log.txt.
      var blob = new Blob(['Lorem Ipsum'], {type: 'text/plain'});

      fileWriter.write(blob);

    }, 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);
}
window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);

I am working on the tutorial for HTML5 FileSystem-API. Basically I would like to write a file to my machine. I am using python's simpleHTTPServer to serve the website. Once I load the webpage, the console doesn't raise an error and I get a message that the write was successful. But I can't find the file anywhere on my machine. Here is my code:

function onInitFs(fs) {

  fs.root.getFile('zz11zzlog.txt', {create: true}, function(fileEntry) {

    // Create a FileWriter object for our FileEntry (log.txt).
    fileEntry.createWriter(function(fileWriter) {

      fileWriter.onwriteend = function(e) {
        console.log('Write pleted.');
      };

      fileWriter.onerror = function(e) {
        console.log('Write failed: ' + e.toString());
      };

      // Create a new Blob and write it to log.txt.
      var blob = new Blob(['Lorem Ipsum'], {type: 'text/plain'});

      fileWriter.write(blob);

    }, 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);
}
window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);
Share Improve this question asked May 25, 2016 at 23:48 gpanterovgpanterov 1,4152 gold badges16 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

But I can't find the file anywhere on my machine.

If the write was successful, the file would be written to local filesystem at chrome or chromium configuration directory at Default -> File System directory.

See How to Write in file (user directory) using JavaScript?

Call this two line for file download

window.requestFileSystem(window.TEMPORARY, fileSize, function(filesystem){
									 setTimeout(function() {
										downloadFs(filesystem,fileName);
									 }, time);
							}, errorHandler);

 function downloadFs(fs,fileName) {
			   fs.root.getFile(fileName, {create: false}, function(fileEntry) {
			        console.log("filesystem:"+fileEntry.toURL()) ;
					window.location.href = fileEntry.toURL();
			   }, errorHandler);	
	 }

发布评论

评论列表(0)

  1. 暂无评论