Phonegap - Error to upload image from photogalery to a server

962 views Asked by At

I'm trying to create a script (Phonegap for Android) to send a selected image from photogalery, but it's going hard to me.

Following my script:

function onCapturePhoto(imageURI) {

    var uri = encodeURI("https://www.myserver.com/webservices/uploadFoto.php");
    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1);
    options.mimeType = "text/jpeg";

    var ft = new FileTransfer();

    ft.upload(imageURI, uri, win, fail, options);

    function win(r) {
        console.log("Code: = " + r.responseCode);
        console.log("Response: = " + r.response);
        console.log("Sent: = " + r.bytesSent);
    }

    function fail(error) {
        console.log("An error has occurred: Code = " + error.code);
        console.log("upload error source: " + error.source);
        console.log("upload error target: " + error.target);
    }
}

function getPhoto() {
    navigator.camera.getPicture(onCapturePhoto, onFail, {
        quality: 80,
        destinationType: navigator.camera.DestinationType.FILE_URI,
        sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,
        allowEdit : true,
        targetWidth: 150,
        encodingType: navigator.camera.EncodingType.JPEG
    });
}

function onFail(message) {
    alert('Failed because: ' + message);
}

Im' getting the following error:

An error has occurred: Code = 1

upload error source: file:///storage/emulated/0/Android/data/mobi.monaca.debugger/cache/.Pic.jpg

upload error target: https://www.myserver.com/webservices/uploadFoto.php

I'm using Monaca Debugger.

1

There are 1 answers

0
Jack He On BEST ANSWER

Wrong code for uploading image:

options.mimeType = "text/jpeg";

Change it to:

options.mimeType = "image/jpeg";

On the other hand, some useful option code for Android 4.4

options.chunkedMode = false;
options.headers = {Connection: "close"};