Mic*_*elS 5 sqlite zip windows-mobile ios cordova
对于目前在Windows Mobile和iOS上运行的Cordova应用程序,我需要一个"支持"模式.为此,我需要压缩一个sqlite数据库文件并将其上传到服务器.数据库必须进行压缩,因为它可能会超过250MB并且上传必须在没有wifi连接的情况下工作.
搜索网络带来了不同的方法,但所有这些方法都已过时或仅解决了iOS或Windows Mobile的问题.例如,在使用Cordova文件插件时,我在插件文档中遇到过这种情况:
支持的平台
Android iOS OS X Windows*浏览器
- 这些平台不支持FileReader.readAsArrayBuffer和FileWriter.write(blob).
这是我的方法:Cordova - 在iOS上压缩文件和文件夹
有任何想法吗?
小智 2
我建议您再给 FileReader() 一次机会。
就我而言,可能与您的情况非常相似,我使用 FilerReader.readAsArrayBuffer 读取文件,然后使用 JSZip 库压缩它:http://stuartk.com/jszip
与 cordova-file-plugin 的 API 文档相反(https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/)“Windows*”->“这些平台不支持 FileReader .readAsArrayBuffer 也不是 FileWriter.write(blob))" 我经历过 readAsArrayBuffer 在 Windows UWP 平台下工作,但速度较慢。
所以就我而言,文件大小约为。50M 整个过程等了近2分钟才完成!
尝试按照这个例子:
您需要适应您的路径,但这适用于 WINDOWS UWP 和 IOS(没有使用 Android 进行测试,但这不是您的问题)。
此外,您还需要实现自己的错误处理程序 (errorHandler)。该解决方案使用 Promises,因为您必须等待文件被读取和压缩。
PS1:始终确保您的“设备就绪事件”已被触发才能访问插件。
PS2:您可能会遇到数据库文件没有访问权限的情况,这可能与该数据库文件正在被另一个进程使用有关。确保数据库已关闭。SQLITE:
var sqlite = window.sqlitePlugin.openDatabase({ name: 'yourdb.db', location: 'default' });
sqlite.close(function () {
console.log("DONE closing db");
}, function (error) {
console.log("ERROR closing db");
console.log(JSON.stringify(error));
});
Run Code Online (Sandbox Code Playgroud)
“邮编”功能:
function zipFile(sourceFileName, targetFileName) {
return new Promise(function (resolve, reject) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
fs.root.getFile(sourceFileName, { create: false, exclusive: false }, function (fe) {
fe.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (data) {
var zip = new JSZip();
zip.file(sourceFileName, data.target.result);
zip.generateAsync({
type: "blob",
compression: "DEFLATE",
compressionOptions: {
level: 9
}
// level 9 means max. compression
// this may also take some time depending on the size of your file
// I tested it with a 50M file, it took about 65 sec.
}).then(
// following is post-zip in order to transfer the file to a server
function (blob) {
fs.root.getFile(targetFileName, { create: true, exclusive: false }, function (newzip) {
writeFile(newzip, blob, "application/zip").then(function () {
var f = blob;
var zipReader = new FileReader();
zipReader.onloadend = function (theFile) {
var base64 = window.btoa(theFile.target.result);
resolve(base64);
};
// need to "resolve" the zipped file as base64 in order to incluse it in my REST post (server-upload)
zipReader.readAsBinaryString(f);
});
});
}
)
};
reader.readAsArrayBuffer(file);
// this may take some time depending on the size of your file
// I tested it with a 50M file, it took about 72 sec.
}, errorHandler);
}, errorHandler);
});
});
}
Run Code Online (Sandbox Code Playgroud)
前电话:
if (window.cordova) {
document.addEventListener('deviceready', function () {
zipFile("yourDatabaseFileName.db","compressedDatabaseFile.zip");
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
538 次 |
| 最近记录: |