【发布时间】:2012-08-01 07:15:56
【问题描述】:
我正在尝试使用相机捕获图像并将其上传到我的 AJAX 端点。我已确认此端点可以接受该文件(我在桌面上创建了一个测试 HTML 文件,该文件发送了一个包含图像的表单)。我正在使用 Cordova (phonegap) 1.7.0,并试图让 fileTransfer() 工作。这是我关注的文档的链接:
http://docs.phonegap.com/en/1.0.0/phonegap_file_file.md.html#FileTransfer
成功回调触发,但在端点上找不到 $_FILES 数据。
然后我找到了这篇文章:
http://zacvineyard.com/blog/2011/03/25/upload-a-file-to-a-remote-server-with-phonegap/
建议使用options.chunkedMode = false。现在上传需要一个半岁,最终失败,错误代码为 3,我相信是FileError.ABORT_ERR。
我错过了什么吗?
我的代码来自下面的应用程序:
navigator.camera.getPicture(function(imageURI){
console.log('take success! uploading...');
console.log(imageURI);
var options = new FileUploadOptions();
options.fileKey = 'file';
options.fileName = 'spot_image.jpeg';
options.mimeType = 'image/jpeg';
var params = new Object();
params.spot_id = 1788;
params.param2 = 'something else';
options.params = params;
options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload(imageURI,serverURL + '/ajax.php?fname=appuploadspotimage',function(r){
console.log('upload success!');
console.log(r.responseCode);
console.log(r.response);
console.log(r.bytesSent);
},function(error){
console.log('upload error')
console.log(error.code);
},options,true);
console.log('after upload');
},function(message){
console.log('fail!');
console.log(message);
},{
quality: 50,
destinationType: navigator.camera.DestinationType.DATA_URL,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
serverURL 被定义为我的 AJAX 端点的域,它已在 cordova.xml 中列入白名单。
我在 SO 中看到了很多关于此的问题,对于是否应该使用 chunkedMode 有不同的看法。有人也有这个问题吗?
我正在运行 ICS 的三星 Galaxy S 上尝试这个。
愿帮助我解决这个问题的人神秘地继承一家啤酒厂。
【问题讨论】:
-
你能在你的服务器中检查它接收到的帖子参数吗?我遇到了类似的问题,最后发现是我没有正确发送选项对象
-
@davids 你的意思是检查服务器中的
$_POST变量吗?我试过了,我得到一个空数组。我也尝试使用$_REQUEST,只是为了检查,只有我在url中提供的fname参数在那里。$_FILES出现一个空数组。