Ionic 3 Native : File : {code: 5, message: “ENCODING_ERR”}

你。 提交于 2020-01-24 04:15:30

问题


I'm using the checkFile function from File API plugin in Ionic 3 to check if a file exists locally. The Promise rejects with the following error :

FileError {code: 5, message: "ENCODING_ERR"}

From what I see on the Mozilla Docs of the File API, the problem is that "The URL is malformed." However, I don't see how is the URL malformed. Here is the relevant code showing how I'm calling the function plus the actual values of involved variables (The value of baseDirectory is set to file:///data/user/0/ch.protectator.fehpedia/files/) :

let baseDirectory = this.file.dataDirectory;
let fileToCheck = "File:Icon Portrait Abel.png";

let promise = this.file.checkFile(this.file.dataDirectory, fileName).then(bool => {
    // Things
}, reason => {
    console.error(reason);
});

And that's the Promise that fails. What's strange about that case is that I actually tried to display that image later in the code, ignoring if it has been found by File.checkFile, and the image displays.

In the HTML template, I later use :

<img [src]="imgUrl">

where imgUrl is set using :

this.imgUrl = this.file.dataDirectory + '/' + "File:Icon Portrait Abel.png";

So the image exists and displays correctly when called by the WebView, but the Native File plugin tells me the URL is malformed, even to me it seems to be exactly the same URL. That's where I'm stuck, I don't know what to change for the code to work. Should checkFile be used in a different way ? Also, a more precise cause of failure would help, but all I got is ENCODING_ERR, I don't know what actual part of the URL seems malformed.


回答1:


After looking through similar cases, I've found no solution nor documentation about this specific case. But while experimenting with different values, I found what caused this error : :. Yes, the colon. The one present in the file name.

While I still have no explanation about why : causes the native File plugin to fail but not the web view, removing : from the file name effectively allows File to see it without raising an ENCODING_ERR. While this is not an optimal solution, this workaround is effective.




回答2:


In my case, I let "File:///" in the route and now it is working.




回答3:


None of the answers solve my problem. It was very simple, I was using the wrong Directory to save a file in iOS. I was using "Cache" but kept getting the error

I change to

this.file.tempDirectory;

And worked!



来源:https://stackoverflow.com/questions/45910519/ionic-3-native-file-code-5-message-encoding-err

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