JIMJI1005
JIMJI1005

Reputation: 486

cordova android image upload failed

Having some issues with getting the cordova filetransfer to work.

I am reading the logs and it seems the file is getting transfered but the server is striping the extension off and writing 0 size. Using blueimp file-upload server handler. The upload handler works on my jquery scripts. I have gotten it to work before. Not sure what changed and all of the sudden its not writing the file correctly.

Here is the log from the 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)

Here is the function

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);
}

Upvotes: 0

Views: 1462

Answers (2)

JIMJI1005
JIMJI1005

Reputation: 486

Went back to blueimp's File Upload Handler. As it turns out it does work.

Cordova Android File Transfer uses 'file' as the key while Upload Hander looks for 'files'. So changing file to files made the PHP Serverside handler works. Now I can use the same handler on both webapp and cordova.

FYI make sure to use JSON PARSE to read the response once you upload.

Full code as follows.

Cordova Camera button

$('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'];


    }
}

Upvotes: 1

JIMJI1005
JIMJI1005

Reputation: 486

Gave up using jquery fileupload as the upload handler. Created a simple PHP upload handler and it works. Just lacking the fancy resize etc.

Upvotes: 0

Related Questions