问题描述
我在 Ionic 3 中使用 file API 插件中的 checkFile 函数检查本地是否存在文件.Promise 拒绝并出现以下错误:
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"}
FileError {code: 5, message: "ENCODING_ERR"}
根据我在 Mozilla Docs of the File APIbaseDirectory 的值设置为 file:///data/user/0/ch.protectator.fehpedia/files/
):
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);
});
这就是失败的 Promise.这种情况的奇怪之处在于,我实际上试图稍后在代码中显示该图像,忽略 File.checkFile 是否已找到它,并且 图像显示.
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.
在 HTML 模板中,我稍后使用:
In the HTML template, I later use :
<img [src]="imgUrl">
其中 imgUrl
使用 :
this.imgUrl = this.file.dataDirectory + '/' + "File:Icon Portrait Abel.png";
所以图像存在并在被 WebView 调用时正确显示,但是 Native File 插件告诉我 URL 格式错误,即使对我来说它似乎是完全相同的 URL.那就是我卡住的地方,我不知道要更改什么才能使代码正常工作.checkFile
应该以不同的方式使用吗?此外,更准确的失败原因会有所帮助,但我得到的只是 ENCODING_ERR
,我不知道 URL 的哪个实际部分看起来格式不正确.
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.
推荐答案
在查看了类似的案例后,我没有找到关于这个特定案例的解决方案或文档.但是在尝试不同的值时,我发现了导致此错误的原因::
.是的,结肠.文件名中的那个.
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.
虽然我仍然无法解释 为什么 :
导致本机 File
插件失败但不是 web 视图,删除 :
从文件名有效地允许 File
看到它而无需引发 ENCODING_ERR
.虽然这不是最佳解决方案,但此解决方法是有效的.
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.
这篇关于Ionic 3 Native:文件:{代码:5,消息:“ENCODING_ERR"}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!