maj21
maj21

Reputation: 43

phonegap filetransfer.upload to send image to server 200 bytes too short

I'm trying to send image files using phonegap's filetransfer.upload, but the returned file is broken and looking at logcat it the sent file seems to be 200 bytes too short.

Here is my code for sending the file

sendImageFile = function (imageURI, imageName) {
    writelog("Sending image file", 1);
    var options = new FileUploadOptions();
            options.fileKey="file";
            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.mimeType="image/jpeg";
            var params = new Object();
            params.value1 = "image name";
            options.params = params;
            options.chunkedMode = false;
            var ft = new FileTransfer();
            writelog("image uri length " + imageURI.length, 1);
    writelog("Image options set up successfully", 1);
    var ft = new FileTransfer();
    ft.upload(imageURI, uploadurl, win, transFail, options);
}

and here are some pertinent lines from logcat

01-07 12:27:30.743: D/FileTransfer(20066): Uploaded 114688 of 145432    bytes 

01-07 12:27:31.571: D/FileTransfer(20066): got response from    server 

01-07 12:27:31.696: D/CordovaLog(20066): Code = 200 

01-07 12:27:31.696: D/CordovaLog(20066): Response = 12099

01-07 12:27:31.696: D/CordovaLog(20066): Sent = 145236

Any help would be greatly appreciated.

Thanks

Matt

Upvotes: 2

Views: 3385

Answers (2)

maj21
maj21

Reputation: 43

Solution found. My server was accepting all the data sent as a file, as opposed to breaking it up from a form submit (i believe). This was causing there to be several lines of text just before the main image data.

To get round this I installed an old fileTransfer plugin (https://github.com/phonegap/phonegap-plugins/tree/master/Android/FileUploader), reverted to version 1.8.1 of phonegap (as i was unsure how to update the older plugin for the moment).

Edited the fileUpload.java file to remove all text before the file to send. Now it is readable by the server

Upvotes: 2

Davor Zlotrg
Davor Zlotrg

Reputation: 6050

You can not use imageUri that you get from camera success callback on Android 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 upload method
            var options = new FileUploadOptions();
            options.fileKey = "file";
            options.fileName = fileName.substr(fileName.lastIndexOf('/')+1);
            //...
        });
    });

}, errorFn, cameraParams);

Upvotes: 1

Related Questions