How to use webkitRequestFileSystem at file: protocol

后端 未结 1 1201
青春惊慌失措
青春惊慌失措 2020-12-04 00:33

According to Exploring the FileSystem APIs at

Browser support & storage limitations

You may need the --allow-file-access-from-fi

相关标签:
1条回答
  • 2020-12-04 01:00

    The initial try received errors at terminal relating to lack of space on device. Received two separate errors code 7 InvalidStateError and code 3 AbortError. Note, chromium was launched in a sandbox at each configuration.

    Was able to achieve expected result of writing to FileSystem at file: protocol by adjusting launch flags to --allow-file-access-from-files and specifying a different chromium configuration folder at --user-data-dir=/newer/version/of/profile/folder; --unlimited-quota-for-files and --unsafely-treat-insecure-origin-as-secure=file:///path/to/directory/* were not necessary to achieve requirement.

    Not entirely certain why original profile folder was using logged errors when attempting to access FileSystem at file: protocol. The folder could have been from a previous version of chromium. Generally launch new or newest version chromium from command-line where chromium folder in configuration directory may have been an older version with preferences already set. When reviewed terminal at one point no space left on disk message was logged in relation to FileSystem when launched using former profile configuration folder. Tried a newer version of chromium profile folder which solved issue.

    Much credit for solution is due to @PatrickEvans for verifying that process was indeed possible; that it was more than likely a user error which was limiting realization of expected result.

    var requestedBytes = 16,
      _grantedBytes;
    
    function errorHandler(e) {
      console.log(e)
    }
    
    function writeFile(fs) {
      console.log(fs)
      fs.root.getFile("file.txt", {
        create: true
      }, function(fileEntry) {
        fileEntry.createWriter(function(fileWriter) {
          fileWriter.onwriteend = function(e) {
            // call `readFile`
            console.log(_grantedBytes);
            window.webkitRequestFileSystem(window.TEMPORARY
                                          , _grantedBytes
                                          , readFile
                                          , errorHandler);
          };
          fileWriter.onerror = errorHandler;
          var blob = new Blob(["abc"], {
            type: "text/plain"
          });
          fileWriter.write(blob);
        }, errorHandler);
      }, errorHandler);
    }
    
    navigator.webkitTemporaryStorage.requestQuota(requestedBytes
    , function(grantedBytes) {
        console.log(grantedBytes);
        _grantedBytes = grantedBytes;
        window.webkitRequestFileSystem(window.TEMPORARY
                                      , grantedBytes
                                      , writeFile
                                      , errorHandler);
    
    }, errorHandler);
    
    function readFile(fs) {
      fs.root.getFile("file.txt", {}, function(fileEntry) {
        fileEntry.file(function(file) {
          var reader = new FileReader();
          reader.onloadend = function(e) {
            console.log(e.target.result, fileEntry.toURL());
          };
          reader.readAsText(file);
        }, errorHandler);
      }, errorHandler);
    }
    
    0 讨论(0)
提交回复
热议问题