-->

EncodingError when cordova-plugin-file

2019-02-15 15:50发布

问题:

I am now developing a cordova app which platform is browser.

I need to access a text file from local file system, so I am using the cordova-plugin-file.

But I failed as the exception in my chrome console like this (There is no CLI error):

code: 5

message: "A URI supplied to the API was malformed, or the resulting Data URL has exceeded the URL length limitations for Data URLs."

name: "EncodingError"

as my code blow:

document.addEventListener('deviceready', dataRead, false);
    function dataRead() {
      window.webkitRequestFileSystem(window.PERSISTENT, 100500*1024*1024, function() {
        window.webkitResolveLocalFileSystemURL("filesystem:" + cordova.file.applicationDirectory + "1111.csv", gotFile, fail);
      },function (e) {
        console.log(e);
      });
    }
    function fail(e) {
      console.log("FileSystem Error");
      console.dir(e);
    }

    function gotFile(fileEntry) {
      fileEntry.file(function(file) {
        var reader = new FileReader();

        reader.onloadend = function(e) {
          console.log("Text is: "+this.result);
        }

        reader.readAsText(file);
      });

    }

Is my URI illegal or for some other reasons?

Can anybody point out why? Or show me a right example.

Any help is appreciated.

回答1:

Good reference point for the file plugin are its auto and manual tests, which can be run in cordova-plugin-test-framework and also the file plugin documentation.

A few points on your question:

  • try to use something less than 100500*1024*1024, for example 10*1024*1024,
  • instead of

window.webkitResolveLocalFileSystemURL("filesystem:" + cordova.file.applicationDirectory + "1111.csv", gotFile, fail);

try to use

window.resolveLocalFileSystemURL("filesystem:" + cordova.file.applicationDirectory + "persistent/1111.csv", gotFile, fail);