简体   繁体   中英

Cordova file transfer image is not uploading the file to server

Please help me to resolve this issue, am try to solve this from last week. Using cordova file transfer plugins am try to upload my image to production server however am getting error code 1.

i checked source and target path both are accessible. Please find my cordova setup versions.

Cordova -> 7.1.0, Phonegap -> 7.1.1, cordova-plugin-file-transfer -> spec=1.7.1

Code:

 function staticpathu_upload() { var fileURL = 'https://example.com/Al_2_1518090802.jpg'; var uri = encodeURI('https://example.com/dummy.php'); var options = new FileUploadOptions(); options.fileKey = "file"; options.fileName = fileURL.substr(fileURL.lastIndexOf('/')+1); options.mimeType = "image/jpeg"; var ft = new FileTransfer(); ft.upload(fileURL, uri, onSuccess, onError, options); function onSuccess(r) { console.log("Code = " + r.responseCode); console.log("Response = " + r.response); console.log("Sent = " + r.bytesSent); } function onError(error) { console.log(error); alert("An error has occurred: Code = " + error.code); console.log("upload error source " + error.source); console.log("upload error target " + error.target); } } 

Error:

FileTransferError {
   code: 1, 
   source: "https://example.com/Al_2_1518090802.jpg", 
   target: "https://example.com/dummy.php", 
   http_status: null, 
   body: null,
}

fileUrl must be a

Filesystem URL representing the file on the device or a data URI. For backwards compatibility, this can also be the full path of the file on the device. (See Backwards Compatibility Notes below)

See the docs

This is propably the reason why a FileTransferError.FILE_NOT_FOUND_ERR (error code 1) is thrown.

This issue with ServerUrl, its not allowing me to post the image to server. I have created dummy.php on server root folder and execute from below code its working fine. It won't work from desktop we have check in mobiles or emulators only.

function uploadphoto(){ 
    navigator.camera.getPicture(uponSuccess, uponFail, { quality: 50,
      sourceType: Camera.PictureSourceType.PHOTOLIBRARY, 
      allowEdit: true,
      destinationType: Camera.DestinationType.FILE_URI
    });

            // Change image source and upload photo to server
    function uponSuccess(imageURI) {

        // Set image source
        var image = document.getElementById('fileuploadimg');
        $$('.gAlbumList').append('<a href="#"><img src="'+imageURI  + '?' + Math.random()+'" /></a>');


        var options = new FileUploadOptions();
        options.fileKey = "file";
        options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
        options.mimeType = "image/jpeg";

        var params = {};
        params.value1 = "test";
        params.value2 = "param";

        options.params = params;
        options.chunkedMode = false;

        var ft = new FileTransfer();

        ft.upload(imageURI, "https://example.com/dummy.php", function(result){
            $$('.error').append('<p>successfully uploaded ' + result.response+'</p>');
        }, function(error){
            $$('.error').append('<p>error : ' + JSON.stringify(error)+'</p>');
        }, options);

    }

    function uponFail(message) {
        $$('.error').append('<p>Failed because: ' + message+'</p>');
    }

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