EncodingError when cordova-plugin-file

∥☆過路亽.° 提交于 2019-12-13 11:35:14

问题


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);



来源:https://stackoverflow.com/questions/34283600/encodingerror-when-cordova-plugin-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!