EncodingError when cordova-plugin-file
Asked Answered
A

1

2

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.

Abaxial answered 15/12, 2015 at 7:46 Comment(0)
P
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);

Perretta answered 16/12, 2015 at 8:37 Comment(4)
Thanks for your answer. I followed your suggestions, but still failed with a same error message. As I read the cordova-plugin-document: Chrome quirks ,It said that: Chrome filesystem is not immediately ready after device ready event.....You can use window.isFilePluginReadyRaised function to check whether event was already raised. And after I checked about this I found the filePluginIsReady event is never fired. Do you have any suggestions about this? Thanks again.Abaxial
By the way, I raise another question about this. If you have any interest or time, you can go there and have a look filePluginIsReady event is never fired in chrome when using cordova-plugin-file .Thank you very much.Abaxial
@SimonBai, I realized that there is a quirk for browser platform that filesystem: prefix is required - and you also need to specify the FS type in the path - please try the updated answer.Perretta
Oops,I have tried to add a filesystem: before, but didn't tried persistent.As I will try it after the weekend:) Good weekend!Abaxial

© 2022 - 2024 — McMap. All rights reserved.