简体   繁体   中英

Phonegap - Error to upload image from photogalery to a server

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM