Phonegap android无法使用fileTransfer上传图像 [英] Phonegap android unable to upload image using fileTransfer
问题描述
我正在尝试使用相机捕捉图像并将其上传到我的 AJAX 端点.我已经确认此端点可以接受该文件(我在桌面上创建了一个测试 HTML 文件,该文件发送一个带有图像的表单).我正在使用 Cordova (phonegap) 1.7.0,并试图让 fileTransfer() 工作.这是我遵循的文档的链接:
I'm trying to capture an image using the camera and upload it to my AJAX endpoint. I've confirmed that this endpoint can accept the file (I created a test HTML file on my desktop that sends a form with an image in it). I'm using Cordova (phonegap) 1.7.0, and am trying to get the fileTransfer() to work. Here is the link for the documentation that I followed:
http://docs.phonegap.com/en/1.0.0/phonegap_file_file.md.html#FileTransfer
成功回调触发,但在端点上找不到 $_FILES 数据.
The success callback triggers, but no $_FILES data is to be found on the endpoint.
然后我找到了这篇文章:
I then found this article:
http://zacvineyard.com/blog/2011/03/25/upload-a-file-to-a-remote-server-with-phonegap/
建议使用 options.chunkedMode = false
.现在上传需要一个半年的时间,最终失败,错误代码为 3,我认为这是 FileError.ABORT_ERR
.
Which suggested using options.chunkedMode = false
. Now the upload takes an age and a half, before eventually failing with an error code of 3, which I believe is FileError.ABORT_ERR
.
我错过了什么吗?
我来自以下应用的代码:
My code from the app below:
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 中列入白名单.
serverURL
is defined as the domain for my AJAX endpoint, which has been whitelisted in cordova.xml.
我在 SO 中看到了许多关于此的问题,对于是否应该使用 chunkedMode 有不同的意见.有人也遇到这个问题吗?
I've seen a number of questions here in SO regarding this, which varying opinions as to whether chunkedMode should be used. Anyone having this issue as well?
我正在运行 ICS 的三星 Galaxy S 上尝试此操作.
Am trying this on a Samsung Galaxy S, running ICS.
愿帮助我解决这个问题的人神秘地继承了一家啤酒厂.
May the person who helps me solve this issue mysteriously inherit a beer factory.
推荐答案
您不能在 FileTransfer 上传方法中使用从相机成功回调中获取的 imageUri,您必须首先将 uri 解析为文件名,如下所示:
You can not use imageUri that you get from camera success callback in FileTransfer upload method, you have to first resolve uri as a filename like this:
navigator.camera.getPicture(function(imageURI){
window.resolveLocalFileSystemURI(imageUri, function(fileEntry) {
fileEntry.file(function(fileObj) {
var fileName = fileObj.fullPath;
//now use the fileName in your method
//ft.upload(fileName ,serverURL + '/ajax.php?fname=appuploadspotimage'...);
});
});
});
这篇关于Phonegap android无法使用fileTransfer上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!