cordova android图片上传失败

时间:2014-01-31 08:37:05

标签: android jquery cordova file-transfer

让cordova文件传输工作有一些问题。

我正在阅读日志,似乎文件正在转移,但服务器正在剥离扩展并写入0大小。使用blueimp文件上传服务器处理程序。上传处理程序适用于我的jquery脚本。我之前已经开始工作了。不确定是什么改变了,而且突然之间没有正确地写文件。

这是来自logcat的日志

D/FileTransfer(22082): Sent 100387 of 100387
D/FileTransfer(22082): response code: 200
D/FileTransfer(22082): response headers: {null=[HTTP/1.1 200 OK], Access-Control
-Allow-Origin=[*], Cache-Control=[no-store, no-cache, must-revalidate], Connecti
on=[close], Content-Disposition=[inline; filename="files.json"], Content-Type=[t
ext/plain], Date=[Fri, 31 Jan 2014 08:28:24 GMT], Pragma=[no-cache], Server=[Apa
che/2.4.7 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4], Transfer-Encoding=[chun
ked], Vary=[Accept], X-Android-Received-Millis=[1391156904056], X-Android-Respon
se-Source=[NETWORK 200], X-Android-Sent-Millis=[1391156902868], X-Content-Type-O
ptions=[nosniff], X-Powered-By=[PHP/5.4.23]}
D/FileTransfer(22082): got response from server
D/FileTransfer(22082): {"files":[{"name":"52eb5ea9309fe","size":0,"type":"multip
art\/form-data;boundary=+++++","error":"abort","deleteUrl":"http:\/\/drayagedev.
fr8taxi.com\/images\/uploader\/?file=52eb5ea9309fe","deleteType":"DELETE"}]}
D/CordovaLog(22082): file:///android_asset/www/js/dashboard_driver.js: Line 305
: Code = 200
I/chromium(22082): [INFO:CONSOLE(305)] "Code = 200", source: file:///android_ass
et/www/js/dashboard_driver.js (305)
D/CordovaLog(22082): file:///android_asset/www/js/dashboard_driver.js: Line 306
: Response = {"files":[{"name":"52eb5ea9309fe","size":0,"type":"multipart\/form-
data;boundary=+++++","error":"abort","deleteUrl":"http:\/\/drayagedev.fr8taxi.co
m\/images\/uploader\/?file=52eb5ea9309fe","deleteType":"DELETE"}]}
I/chromium(22082): [INFO:CONSOLE(306)] "Response = {"files":[{"name":"52eb5ea930
9fe","size":0,"type":"multipart\/form-data;boundary=+++++","error":"abort","dele
teUrl":"http:\/\/**.com\/images\/uploader\/?file=52eb5ea9309fe",
"deleteType":"DELETE"}]}", source: file:///android_asset/www/js/dashboard_driver
.js (306)
D/CordovaLog(22082): file:///android_asset/www/js/dashboard_driver.js: Line 307
: Sent = 98416
I/chromium(22082): [INFO:CONSOLE(307)] "Sent = 98416", source: file:///android_a
sset/www/js/dashboard_driver.js (307)

这是函数

function uploadPhoto(imageURI) {
    var options = new FileUploadOptions();
    options.fileKey="file";
    options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
    options.mimeType="image/jpeg";
    options.headers = {
        Connection: "close"
    }
    options.chunkedMode = false;

    var params = {};
    params.fullpath = imageURI;
    params.name = options.fileName;

    //options.params = params; Uncommenting has no effect

    var ft = new FileTransfer();
    ft.upload(imageURI, encodeURI("http://**/images/uploader"), checkUploadStatus, checkUploadStatus, options);
}

2 个答案:

答案 0 :(得分:1)

回到blueimp的文件上传处理程序。事实证明它确实有效。

Cordova Android文件传输使用'文件'作为键,而上传汉字查找'文件'。因此,将文件更改为文件使PHP Serverside处理程序正常工作。现在我可以在webapp和cordova上使用相同的处理程序。

FYI确保在上传后使用JSON PARSE读取响应。

完整代码如下。

科尔多瓦   相机按钮

$('body').on('click', '.btn_upload', function (){

    navigator.camera.getPicture(uploadPhoto, onFail, { quality: 75,
        destinationType: Camera.DestinationType.FILE_URI
    });

})

function onFail() {

 }


function uploadPhoto(imageURI) {

startSpin();

var options = new FileUploadOptions();
options.fileKey="files";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
options.headers = {
    Connection: "close"
}
options.chunkedMode = false;

var params = {};
params.fullpath = imageURI;
params.name = options.fileName;

//options.params = params;

var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("http://**/images/uploader/index.php"), win, onFail, options);

}


 function win(result) {

if(result.responseCode == "200"){

    var result = jQuery.parseJSON(result.response);

    var filename = result.files[0]['name'];
    var filethumbnailUrl = result.files[0]['thumbnailUrl'];
    var filelargeUrl = result.files[0]['largeUrl'];
    var filemediumUrl = result.files[0]['mediumUrl'];
    var fileurl = result.files[0]['url'];


    }
}

答案 1 :(得分:0)

使用jquery fileupload作为上传处理程序。创建一个简单的PHP上传处理程序,它的工作原理。只是缺乏花哨的调整大小等。