0
我有一个非常简单的应用程序,可以从相机和库上传图片。我设法按预期工作,出于某种原因,我停止了工作。我甚至从一个提交下载了文件,我确定它正在工作,它现在不在了。使用phonegap上传文件到服务器的问题cordova
该应用程序让你拍照的,从其中选择一个画廊,它则增加了一个小的缩略图(这工作)的图像拍摄/选择让你提交表单。选择文件后,上传就会立即生效,并且表单提交将包含这些文件的电子邮件作为附件发送(在服务器上完成)。
上传功能总是返回“发生了一个错误:代码= 1”
编辑:我通过我的设备连接到PhoneGap的Windows服务器测试应用。
EDIT2:显然这个问题是PhoneGap的模拟器,试图在真实的apk安装和它的工作。
CODE:
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
// Now safe to use device APIs
//alert("Device Ready");
/*button that opens camera and takes picture*/
$("#add_photo").click(function(){
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
sourceType: Camera.PictureSourceType.CAMERA,
destinationType: Camera.DestinationType.FILE_URI });
});
/*on success show thumbnail and try to upload*/
function onSuccess(imageURI) {
$("#preview").append("<img src='"+imageURI+"'/>");
fileUpload(imageURI, "image/jpeg");
}
function onFail(message) {
alert('Failed because: ' + message);
}
function fileUpload(imageURI, mimeType){
var win = function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
$('#form_submit').prop('disabled', false);
$('#form_submit').prop('value', 'ENVIAR');
//alert("done");
//alert(r.response);
}
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);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
options.mimeType = mimeType;
var params = {};
i++;
sent_files[i] = session_id+"-"+i+"."+imageURI.split('.').pop();
params.session_id = sent_files[i];
$('[name=send]').val(sent_files.join());
options.params = params;
$('#form_submit').prop('disabled', true);
$('#form_submit').prop('value', 'CARGANDO');
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("http://myServer/folder"), win, fail, options);
};
}
试图在真实设备安装目录,而不是使用PhoneGap的仿真器和它的工作如预期。 –