【发布时间】:2018-04-16 16:52:14
【问题描述】:
我正在尝试获取文件传输插件的示例代码,它直接取自 Cordova 文档:
function downloadFile2() {
window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
console.log('file system open: ' + fs.name);
// Make sure you add the domain name to the Content-Security-Policy <meta> element.
var url = 'http://cordova.apache.org/static/img/cordova_bot.png';
// Parameters passed to getFile create a new file or return the file if it already exists.
fs.root.getFile('downloaded-image.png', { create: true, exclusive: false }, function (fileEntry) {
download2(fileEntry, url, true);
}, function () { logError('Error creating file'); });
}, function () { logError('Error creating fs'); });
}
function download2(fileEntry, uri, readBinaryData) {
var fileTransfer = new FileTransfer();
var fileURL = fileEntry.toURL();
console.log('Downloading ' + uri + ' to ' + fileURL);
fileTransfer.download(
uri,
fileURL,
function (entry) {
console.log("Successful download...");
console.log("download complete: " + entry.toURL());
if (false && readBinaryData) {
// Read the file...
readBinaryFile(entry);
}
else {
// Or just display it.
displayImageByFileURL(entry);
}
},
function (error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
null, // or, pass false
{
//headers: {
// "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
//}
}
);
}
function displayImageByFileURL(fileEntry) {
var elem = document.getElementById('imageElement');
elem.src = fileEntry.toURL();
}
我正在使用最新版本的文件传输和文件插件 (1.7.1/6.0.1)。如示例中所述,我已将域添加到 Content-Security-Policy 元素:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: http://cordova.apache.org https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
当我在 VS2017 的模拟器 (Android/iOS) 中运行它时,下载会静默失败。既没有调用成功回调也没有调用错误回调,并且它似乎没有生成网络请求。控制台日志如下:
file system open: http_localhost_4400:Temporary
Downloading http://cordova.apache.org/static/img/cordova_bot.png to filesystem:http://localhost:4400/temporary/downloaded-image.png
那个文件系统 URL 对我来说有点奇怪,所以我尝试了其他变体,例如完整文件路径,使用持久存储而不是临时存储,使用 'cdvfile://localhost/persistent/downloaded-image.png',都具有相同的结果。我不知道如何进一步调试它,并想知道我是否错过了一些非常明显的东西,所以任何建议都值得赞赏......
编辑 我今天再次尝试运行它,Visual Studio 中弹出一个对话框,其中包含以下消息:
以下 exec 调用没有处理程序:
FileTransfer.download("http://cordova.apache.org/static/img/cordova_bot.png", "cdvfile://localhost/persistent/downloaded-image.png", true, 1, null)
【问题讨论】:
标签: cordova-plugins visual-studio-cordova