Cordova 正在“日落”(即将弃用)cordovan-plugin-file,请参阅他们的博文.
Cordova 开发社区不会再对文件传输插件进行更多工作。
如果您愿意,您可以继续使用文件传输插件 - 在可预见的将来它应该可以正常工作。
我们强烈建议 Cordova 用户转而使用符合标准的方式发送和接收二进制数据。
他们鼓励过渡到使用 XHR2 请求(XHR 请求,其中响应类型被设定为Blob or 数组缓冲区.
该博文想要提供一个如何使用 XHR2 获取二进制数据的示例:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
console.log('file system open: ' + fs.name);
fs.root.getFile('bot.png', { create: true, exclusive: false }, function (fileEntry) {
console.log('fileEntry is file? ' + fileEntry.isFile.toString());
var oReq = new XMLHttpRequest();
// Make sure you add the domain name to the Content-Security-Policy <meta> element.
oReq.open("GET", "http://cordova.apache.org/static/img/cordova_bot.png", true);
// Define how you want the XHR data to come back
oReq.responseType = "blob";
oReq.onload = function (oEvent) {
var blob = oReq.response; // Note: not oReq.responseText
if (blob) {
// Create a URL based on the blob, and set an <img> tag's src to it.
var url = window.URL.createObjectURL(blob);
document.getElementById('bot-img').src = url;
// Or read the data with a FileReader
var reader = new FileReader();
reader.addEventListener("loadend", function() {
// reader.result contains the contents of blob as text
});
reader.readAsText(blob);
} else console.error('we didnt get an XHR response!');
};
oReq.send(null);
}, function (err) { console.error('error getting file! ' + err); });}, function (err) { console.error('error getting persistent fs! ' + err); });
我在理解上面的代码以及科尔多瓦放弃文件传输插件以支持的意图时遇到一些问题
通过 Ajax 直接获取 Blob。
我这样看对吗:fs.root.getFile
创建一个文件。下载成功处理程序(oReq.onload
)不尝试
将获取的 blob 写入创建的文件。没有明确的原因创建 fileEntry。
如果我想将获取的 blob 保存到创建的 FileEntry 中,请在oReq.onload
我可以继续使用 FileWriter,但仅限于小文件(我最多读取 5 MB)(因为 Blob 是在内存中处理的)。
该博客文章更多地介绍了如何获取一般的 blob,而不是它可以
被下载到文件系统中。如果我想下载更大的文件(比如几个 100 MB),
目前,放弃 cordova-plugin-filetransfer 并不是一个选择。