使用 XHR2 请求而不是 cordova-file-transfer 将二进制数据下载到应用程序沙箱中
Download binary data into the app sandbox using XHR2 request instead of cordova-file-transfer
Cordova 是 "sunsetting"(即将弃用)cordovan-plugin-file,请参阅 their blogpost。
No more work will be done on the file-transfer plugin by the Cordova development community.
You can continue to use the file-transfer plugin if you wish - it should work fine as-is for the foreseeable future.
We highly suggest Cordova users transition to using the standards-compliant way of sending and receiving binary data.
他们鼓励过渡到使用 XHR2 请求(responseType 设置为 Blob 或 的 XHR 请求ArrayBuffer.
博客 post 想提供一个示例,说明如何使用 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); });
我在理解上面的代码以及 cordova 放弃文件传输插件以支持的意图时遇到了一些问题
通过 Ajax.
直接获取 Blob
我没看错吗:
fs.root.getFile
创建一个文件。下载成功处理程序 (oReq.onload
) 不会尝试
将获取的 blob 写入创建的文件。没有明确的原因创建 fileEntry。
如果我想将获取的 blob 保存到创建的 FileEntry,在 oReq.onload
内
我可以继续使用 FileWriter,但仅限于小文件(我最多读取 5 MB)(因为 Blob 是在内存中处理的)。
博客 post 更多的是关于一般情况下如何获取 blob 而不是关于它可以
被下载到文件系统中。如果我想下载更大的文件(比如 100 MB),
离开 cordova-plugin-filetransfer 目前不是一个选项。
使用此代码,您可以下载大图像,因为它们是由 1MB 的块写入的,而不是一次完成整个写入。
没有 1MB 的写入,我无法写入大于 4MB 的文件,但是有了这个,我测试了最大 40MB 的文件没有问题
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory,
function (dirEntry) {
console.log('file system open: ' + dirEntry.name);
createFile(dirEntry, "downloadedImage.jpg");
}, onFSError);
function onFSError(error) {
alert(JSON.stringify(error));
}
function createFile(dirEntry, fileName) {
// Creates a new file or returns the file if it already exists.
dirEntry.getFile(fileName, {create: true, exclusive: false}, function(fileEntry) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://static.vix.com/es/sites/default/files/styles/large/public/imj/3/30-cosas-de-los-gatos-que-no-sabias-3.jpg', true);
xhr.responseType = 'blob';
xhr.onload = function() {
if (this.status == 200) {
var blob = new Blob([this.response], { type: 'image/jpeg' });
writeFile(fileEntry, blob);
}
};
xhr.send();
}, onFSError);
}
function writeFile(fileEntry, data) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function (fileWriter) {
fileWriter.onerror = function(e) {
console.log("Failed file write: " + e.toString());
};
function writeFinish() {
function success(file) {
alert("Wrote file with size: " + file.size);
}
function fail(error) {
alert("Unable to retrieve file properties: " + error.code);
}
fileEntry.file(success, fail);
}
var written = 0;
var BLOCK_SIZE = 1*1024*1024; // write 1M every time of write
function writeNext(cbFinish) {
fileWriter.onwrite = function(evt) {
if (written < data.size)
writeNext(cbFinish);
else
cbFinish();
};
if (written) fileWriter.seek(fileWriter.length);
fileWriter.write(data.slice(written, written + Math.min(BLOCK_SIZE, data.size - written)));
written += Math.min(BLOCK_SIZE, data.size - written);
}
writeNext(writeFinish);
});
}
Cordova 是 "sunsetting"(即将弃用)cordovan-plugin-file,请参阅 their blogpost。
No more work will be done on the file-transfer plugin by the Cordova development community. You can continue to use the file-transfer plugin if you wish - it should work fine as-is for the foreseeable future. We highly suggest Cordova users transition to using the standards-compliant way of sending and receiving binary data.
他们鼓励过渡到使用 XHR2 请求(responseType 设置为 Blob 或 的 XHR 请求ArrayBuffer.
博客 post 想提供一个示例,说明如何使用 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); });
我在理解上面的代码以及 cordova 放弃文件传输插件以支持的意图时遇到了一些问题
通过 Ajax.
我没看错吗:
fs.root.getFile
创建一个文件。下载成功处理程序 (oReq.onload
) 不会尝试
将获取的 blob 写入创建的文件。没有明确的原因创建 fileEntry。
如果我想将获取的 blob 保存到创建的 FileEntry,在 oReq.onload
内
我可以继续使用 FileWriter,但仅限于小文件(我最多读取 5 MB)(因为 Blob 是在内存中处理的)。
博客 post 更多的是关于一般情况下如何获取 blob 而不是关于它可以
被下载到文件系统中。如果我想下载更大的文件(比如 100 MB),
离开 cordova-plugin-filetransfer 目前不是一个选项。
使用此代码,您可以下载大图像,因为它们是由 1MB 的块写入的,而不是一次完成整个写入。 没有 1MB 的写入,我无法写入大于 4MB 的文件,但是有了这个,我测试了最大 40MB 的文件没有问题
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory,
function (dirEntry) {
console.log('file system open: ' + dirEntry.name);
createFile(dirEntry, "downloadedImage.jpg");
}, onFSError);
function onFSError(error) {
alert(JSON.stringify(error));
}
function createFile(dirEntry, fileName) {
// Creates a new file or returns the file if it already exists.
dirEntry.getFile(fileName, {create: true, exclusive: false}, function(fileEntry) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://static.vix.com/es/sites/default/files/styles/large/public/imj/3/30-cosas-de-los-gatos-que-no-sabias-3.jpg', true);
xhr.responseType = 'blob';
xhr.onload = function() {
if (this.status == 200) {
var blob = new Blob([this.response], { type: 'image/jpeg' });
writeFile(fileEntry, blob);
}
};
xhr.send();
}, onFSError);
}
function writeFile(fileEntry, data) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function (fileWriter) {
fileWriter.onerror = function(e) {
console.log("Failed file write: " + e.toString());
};
function writeFinish() {
function success(file) {
alert("Wrote file with size: " + file.size);
}
function fail(error) {
alert("Unable to retrieve file properties: " + error.code);
}
fileEntry.file(success, fail);
}
var written = 0;
var BLOCK_SIZE = 1*1024*1024; // write 1M every time of write
function writeNext(cbFinish) {
fileWriter.onwrite = function(evt) {
if (written < data.size)
writeNext(cbFinish);
else
cbFinish();
};
if (written) fileWriter.seek(fileWriter.length);
fileWriter.write(data.slice(written, written + Math.min(BLOCK_SIZE, data.size - written)));
written += Math.min(BLOCK_SIZE, data.size - written);
}
writeNext(writeFinish);
});
}