我一直在Phonegap中实现上传图片功能。 我的服务器端代码已准备就绪。
我安装了插件File和FileTransfer但是当我尝试将图片上传到我的服务器时,代码总是失败并且总是会转到fail()函数。我无法确定错误,因为error.code,error.source,error.target始终为null。
为什么我在error.code上得到null?我无法确定这里的错误,所以我真的需要你的帮助。
这是我的代码:
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();
答案 0 :(得分:2)
修正了config.xml
我用过
<gap:plugin name="org.apache.cordova.file"/>
<gap:plugin name="org.apache.cordova.file-transfer"/>
而不是
<feature name="File">
<param name="android-package" value="org.apache.cordova.FileUtils" />
</feature>
<feature name="FileTransfer">
<param name="android-package" value="org.apache.cordova.FileTransfer" />
</feature>
我正在使用适用于Android的phonegap 3.4.0