简体   繁体   English

Cordova 文件插件 FileReader.readAsText 获取错误但不调用错误回调

[英]Cordova File plugin FileReader.readAsText gets error but does not call error callback

I am using Cordova File plugin to read a JSON on local filesystem.我正在使用 Cordova 文件插件在本地文件系统上读取 JSON。 It works perfectly except for some file where I get an error :除了一些我收到错误的文件外,它工作得很好:

SyntaxError: Unexpected EOF

These files probably have wrong characters and I need to fix them but my problem is not there.这些文件可能有错误的字符,我需要修复它们,但我的问题不存在。 My problem is that the plugin logs the error (on the javascript side) but never calls any error callback !!!我的问题是插件记录了错误(在 javascript 端)但从不调用任何错误回调!!!

When I debug and I follow step by step, on the javascript side I get to the exec() command in FileReader.js, I then follow step by step in objectiveC and it seems perfectly fine, but when ObjectiveC returns it results back with its sendPluginResult, I get this error message in javascript console and no error callback is thrown....当我调试并逐步执行时,在 javascript 方面,我到达 FileReader.js 中的 exec() 命令,然后我在 ObjectiveC 中逐步执行,看起来完全没问题,但是当 ObjectiveC 返回时,结果返回sendPluginResult,我在 javascript 控制台中收到此错误消息,并且没有抛出错误回调....

Anybody ever experienced this ?有人经历过吗? Any idea to catch the error ?任何想法来捕捉错误?

Thank you very much.非常感谢。

I don't know if you solved your problem, but I had a similar issue, which was solved by changing $cordovaFile.readAsText to $cordovaFile.readAsBinaryString , with no other changes.我不知道,如果你解决你的问题,但我也有类似的问题,这是通过改变解决了$cordovaFile.readAsText$cordovaFile.readAsBinaryString ,没有其他变化。 No more EOF problems.不再有 EOF 问题。

I've hunted the invisible characters, removed newlines and escaped everything possible.我已经找到了隐形字符,删除了换行符并尽可能地逃避了一切。 Changing the file reader to readAsBinaryString solved the problem.将文件阅读器更改为readAsBinaryString解决了这个问题。

There is a known bug in the Cordova file plugin's readAsText method , which means that files larger than 256 kB containing non-ASCII characters may fail with this error message. Cordova 文件插件的readAsText方法中存在一个已知错误,这意味着包含非 ASCII 字符的大于 256 kB 的文件可能会失败并显示此错误消息。

readAsText() reads files in 256 kB chunks. readAsText()以 256 kB 的块读取文件。 With variable-width encodings like UTF-8 , if there's a multibyte character straddling the chunk boundary, then it will be split into two different byte sequences, each of which will be treated as a character.对于像UTF-8这样的可变宽度编码,如果有一个跨块边界的多字节字符,那么它将被分成两个不同的字节序列,每个序列都将被视为一个字符。

When the JS environment tries to concatenate the first hunk with such a split character, it sees a byte sequence that's not allowed in a string and throws a SyntaxError.当 JS 环境尝试将第一个大块与这样的拆分字符连接起来时,它看到字符串中不允许出现的字节序列并抛出 SyntaxError。

Since there is no test data attached to this question, I can't be sure this is what happened here, but it seems likely.由于此问题没有附加测试数据,因此我无法确定这是此处发生的情况,但似乎很有可能。 The symptoms line up.症状排成一行。

The GitHub issue I linked above suggests a workaround, which is to use the readAsArrayBuffer() method and to use the browser built-in TextDecoder.decode() method to convert to the correct encoding for your file.我上面链接的 GitHub 问题提出了一种解决方法,即使用readAsArrayBuffer()方法并使用浏览器内置的TextDecoder.decode() 方法将文件转换为正确的编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自“ HTML5 File API中的FIleReader.readAsText如何工作”的FileReader错误 - FileReader Error from “How FIleReader.readAsText in HTML5 File API works” 将FileReader.readAsText()结果字符串转换回文件对象以供下载? - Convert FileReader.readAsText() result string back into a file object for download? 如何在 JavaScript 中同步使用 FileReader.readAsText 读取文件? - How to read file using FileReader.readAsText synchronously in JavaScript? HTML5 File API 中的 FileReader.readAsText 如何工作? - How FileReader.readAsText in HTML5 File API works? 使用 FileReader.readAsText() 将 a.txt 文件拆分为 JavaScript 数组 - Splitting a .txt file into a JavaScript Array using FileReader.readAsText() HTML5 文件 API:FileReader.readAsText() 返回“未定义” - HTML5 File API: FileReader.readAsText() returns "undefined" fileReader.readAsText()JavaScript:未显示结果 - fileReader.readAsText() javascript: result not displayed fileReader.readAsText 返回一个带引号的字符串 - fileReader.readAsText returns a string with quote 抛出'FileReader'的fileReader.readAsText():参数1不是'Blob'类型 - fileReader.readAsText() throwing 'FileReader': parameter 1 is not of type 'Blob' 在cordova-plugin-file(iOS)的FileReader上调用readAsArrayBuffer方法时出现内存不足错误 - out of memory error when calling readAsArrayBuffer method on FileReader of the cordova-plugin-file (iOS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM