gpt4 book ai didi

javascript - CORDOVA 文件插件未捕获类型错误 : Illegal constructor

转载 作者:行者123 更新时间:2023-12-03 04:09:37 26 4
gpt4 key购买 nike

我正在尝试使用 cordova 文件插件编写一个文件,以下示例来自 https://www.neontribe.co.uk/cordova-file-plugin-examples/

这是我的代码:

var errorHandler = function (fileName, e) {  
var msg = '';

switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'Storage quota exceeded';
break;
case FileError.NOT_FOUND_ERR:
msg = 'File not found';
break;
case FileError.SECURITY_ERR:
msg = 'Security error';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'Invalid modification';
break;
case FileError.INVALID_STATE_ERR:
msg = 'Invalid state';
break;
default:
msg = 'Unknown error';
break;
};

console.log('Error (' + fileName + '): ' + msg);
}

function writeToFile(fileName, data) {
data = JSON.stringify(data, null, '\t');
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (directoryEntry) {
directoryEntry.getFile(fileName, { create: true }, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function (e) {
// for real-world usage, you might consider passing a success callback
console.log('Write of file "' + fileName + '"" completed.');
};

fileWriter.onerror = function (e) {
// you could hook this up with our global error handler, or pass in an error callback
console.log('Write failed: ' + e.toString());
};

var blob = new Blob([data], { type: 'text/plain' });
fileWriter.write(blob);
}, errorHandler.bind(null, fileName));
}, errorHandler.bind(null, fileName));
}, errorHandler.bind(null, fileName));
}

writeToFile('example.json', { foo: 'bar' });

错误:

Error in Success callbackId: File681009815 : SyntaxError: Unexpected end of input:312

Uncaught SyntaxError: Unexpected end of input:314

Error in Success callbackId: File681009817 : TypeError: Illegal constructor:312

Uncaught TypeError: Illegal constructor:314

知道代码有什么问题吗?

谢谢,菲利普

最佳答案

我猜您正在 Android 4.x 上测试此功能。我在 Android 4.2.2 上也遇到了同样的问题。

首先,注释掉platforms/android/platform_ww/cordova.js中的try{}catch{}第 289,312-314 行,了解代码失败的确切行。它可能会在var blob = new Blob([data], { type: 'text/plain' });

尝试在不使用 Blob() 的情况下直接将数据写入 FileWriter :

带有 Blob

var blob = new Blob([data], { type: 'text/plain' });
fileWriter.write(blob);

没有 Blob

fileWriter.write(data);

native api 写入一个字符串,因此只要您写入文本,它就应该起作用: https://github.com/apache/cordova-plugin-file/blob/master/src/android/FileUtils.java#L1129

另外,我会检查您尝试写入的数据是否正确,主要是通过以下错误:

Unexpected end of input

最后,这个解决方案可能与这个问题有关: https://stackoverflow.com/a/15302872/1786341

关于javascript - CORDOVA 文件插件未捕获类型错误 : Illegal constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44383100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com