所以我正在开发一个cordova应用程序,它使用cordova-plugin-file和cordova-plugin-file-transfer下载图像,然后将其用作用户登录的个人资料图片。我的index.js是:
//THIS IS CUSTOM LOG FUNCTION PLEASE IGNORE
var currentdate = new Date();
var datetime = currentdate.getDate() + "/" +
(currentdate.getMonth() + 1) + "/" +
currentdate.getFullYear() + " - " +
currentdate.getHours() + ":" +
currentdate.getMinutes() + ":" +
currentdate.getSeconds();
function logit(x) {
console.log(x);
var oldlogs = localStorage.getItem("LOG");
localStorage.setItem("LOG", oldlogs + "\n" + datetime + " >> " + x);
}
//CUSTOM LOG FUNCTION END
function downloadFile() {
logit("dp 1");
var fileTransfer = new FileTransfer(); //this is index.js:37
var uri = encodeURI('http://sachinchoolur.github.io/lightGallery/static/img/thumb-5.jpg');
var fileURL = cordova.file.applicationStorageDirectory + MD5(promail) + ".png";
logit("dp 2");
fileTransfer.download(
uri, fileURL,
function (entry) {
logit("download complete: " + entry.toURL());
$('#proimg').attr('src', cordova.file.applicationStorageDirectory + MD5(promail) + ".png");
},
function (error) {
logit("download error source " + error.source);
logit("download error target " + error.target);
logit("download error code" + error.code);
},
false, {}
);
}
var login = localStorage.getItem("login");
if (login != "true") {
logit("no login detcted");
document.getElementById("profile-box").style.display = "none";
document.getElementById("login-box").style.display = "block";
} else {
var proname = localStorage.getItem("name");
var promail = localStorage.getItem("email");
logit("Logged in already");
document.getElementById("profile-box").style.display = "block";
document.getElementById("login-box").style.display = "none";
document.getElementById("proname").innerHTML = proname;
document.getElementById("promail").innerHTML = promail;
var proimgsrc = document.getElementById("proimg").getAttribute('src');
$(document).ready(function () {
downloadFile(); //this is index.js:74
});
}
使用phonegap服务时,控制台抛出错误;
Logged in already
dp 1
Uncaught ReferenceError: FileTransfer is not defined
at downloadFile (index.js:37)
at HTMLDocument.<anonymous> (index.js:74)
at j (jquery-2.1.1.min.js:2)
at Object.fireWith [as resolveWith] (jquery-2.1.1.min.js:2)
at Function.ready (jquery-2.1.1.min.js:2)
at HTMLDocument.I (jquery-2.1.1.min.js:2)
但我相信没有systax错误,因为当我点击id =&#34的按钮时代码有效;下载&#34;;
<li><a class="btn-floating green" id="download"><i class="material-icons">download</i></a></li>
<script type="text/javascript" src="js/index.js"></script>
<script>
$("#download").click(function() {
downloadFile();
});
</script>
当我点击下载时,控制台说:
dp 1
dp 2
download error source http://sachinchoolur.github.io/lightGallery/static/img/thumb-5.jpg
download error target null2a7844139ea9422d59a6beac69adebe1.png download
error code1
这完全没问题,因为浏览器无法识别cordova.file.applicationStorageDirectory,下载链接也可以在真实设备上进行测试时运行,并且可以正常下载图像。 也许一些设备准备问题? 编辑1:我在index.html中包含了index.js