【发布时间】:2014-05-07 06:55:24
【问题描述】:
我一直在 Phonegap 中实现上传图片功能。 我的服务器端代码已经准备好了。
我已经安装了插件 File 和 FileTransfer,但是当我尝试将图片上传到我的服务器时,代码总是失败并且总是会转到 fail() 函数。我无法确定错误是什么,因为 error.code、error.source、error.target 始终为 null。
为什么我在 error.code 上得到空值?我无法确定这里出了什么问题,所以我真的需要你的帮助。
这是我的代码:
var pictureSource = navigator.camera.PictureSourceType;
var destinationType = navigator.camera.DestinationType;
function clearCache() {
navigator.camera.cleanup();
}
var retries = 0;
function onCapturePhoto(fileURI) {
var win = function (r) {
clearCache();
retries = 0;
alert('Done!');
console.log(r);
}
var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
if (retries == 0) {
retries ++
setTimeout(function() {
onCapturePhoto(fileURI)
}, 1000)
} else {
retries = 0;
clearCache();
alert('Ups. Something wrong happened!');
}
}
var options = new FileUploadOptions();
options.fileKey = "profilepic";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
options.chunkedMode = false;
options.params = {atoken : app.atoken, user_id : window.localStorage.getItem("user_id")}; // if we need to send parameters to the server request
var ft = new FileTransfer();
//alert(app.base_url + "/apiuser/updatePic");
ft.upload(fileURI, encodeURI(app.base_url + "/apiuser/updatePic"), win, fail, options);
}
function capturePhoto() {
navigator.camera.getPicture(onCapturePhoto, onFail, {
quality: 100,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,
destinationType: destinationType.FILE_URI
});
}
function onFail(message) {
alert('Failed because: ' + message);
}
capturePhoto();
【问题讨论】:
标签: cordova phonegap-plugins file-transfer cordova-plugins error-code