当使用cordova-plugin-file时,filePluginIsReady事件不会在chrome中触发 [英] filePluginIsReady event is never fired in chrome when using cordova-plugin-file
问题描述
我现在正在开发一个cordova应用程序whitch平台是浏览器(Chrome)。
使用cordova-plugin-file读取文件时失败。 p>
根据 cordova的文件-plugin-file:Chrome怪癖,它说:
Chrome文件系统在设备就绪事件后不会立即准备就绪。作为解决方法,您可以订阅 filePluginIsReady
event ....您可以使用 window.isFilePluginReadyRaised
函数检查事件
我写的代码如下:
document.addEventListener('deviceready',dataRead,false);
function dataRead(){
window.addEventListener('filePluginIsReady',readyToRead,false);
console.log(window.isFilePluginReadyRaised());
}
function readyToRead(){
window.initPersistentFileSystem(10 * 1024 * 1024,function(){
var fs = cordova.file.applicationDirectory;
console.log(fs);
window.resolveLocalFileSystemURL(cordova.file.applicationDirectory +www / 1111.csv,gotFile,fail);
},function(e){
console.log(e);
});
}
function fail(e){
console.log(FileSystem Error);
console.dir(e);
}
function getFile(fileEntry){
fileEntry.file(function(file){
var reader = new FileReader();
reader.onloadend = function(e){
console.log(Text is:+ this.result);
}
reader.readAsText(file);
});
}
无法读取该文件,控制台中的消息如下: / p>
为文件添加代理------------------ cordova.js:942
授予持久性fs配额--------- Preparing.js:170
false --- ----------------------------------- app.js:4(
值) .isFilePluginReadyRaised()
)
似乎 filePluginIsReady
事件没有发射! WHY?
此外,如果我直接在 deviceready
事件下写我的代码。它也会失败,并显示以下错误讯息:
代码:5
:提供给API的URI格式错误,或者结果数据网址超过了数据网址的网址长度限制。
name:EncodingError
任何人都可以指出为什么或给我一个正确的例子?
任何帮助
为什么会发生这种情况的一个原因可能是你附加到filePluginIsReady的事件已经被触发。尝试订阅appStart.js中的事件或应用中的第一个文件。它为我工作
I am now developing a cordova app whitch platform is browser(Chrome).
I failed when using the cordova-plugin-file to read a file.
According to the document of cordova-plugin-file : Chrome quirks, It said that:
Chrome filesystem is not immediately ready after device ready event. As a workaround you can subscribe to filePluginIsReady
event....You can use window.isFilePluginReadyRaised
function to check whether event was already raised.
I wrote my code like this :
document.addEventListener('deviceready', dataRead, false);
function dataRead() {
window.addEventListener('filePluginIsReady', readyToRead, false);
console.log(window.isFilePluginReadyRaised());
}
function readyToRead(){
window.initPersistentFileSystem(10*1024*1024, function() {
var fs = cordova.file.applicationDirectory;
console.log(fs);
window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + "www/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);
});
}
It failed to read the file, and the message in console likes this:
adding proxy for File ------------------cordova.js:942
Persistent fs quota granted ---------Preparing.js:170
false --------------------------------------app.js:4 (value of
window.isFilePluginReadyRaised()
)
It seems that filePluginIsReady
event didn't fired! WHY?
Besides, if I write my code under the deviceready
event directly. It will also fail with error message below:
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"
Can any one point out why or show me a right example?
Any help is appreciated.
one thing why this could have happened is that probably the time you attached to the filePluginIsReady the event was already fired. Try to subscribe to the event in the appStart.js or the first file in your app. It worked for me
这篇关于当使用cordova-plugin-file时,filePluginIsReady事件不会在chrome中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!