简体   繁体   中英

In cordova project I am using file transfer plugin to upload a profile photo it always returns Code 1 error?

I am trying to upload photo using cordova file transfer plugin. But is always returning Code 1 error which is FILE_NOT_FOUND error but the camera plugin is returning the imageURI perfectly OK. And I have tested my code in postman there it is working fine. But in javascript I am getting code 1 error. I have also tried to hard-code the image URI but the error remains same. Can anyone tell me what's the problem? Here is the snippet of what is I am doing:-

navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
            mediaType: Camera.MediaType.PICTURE,
            destinationType: Camera.DestinationType.FILE_URI,
            sourceType: Camera.PictureSourceType.PHOTOLIBRARY
        });

        function  onPhotoURISuccess(imageURI) {
            alert(imageURI);

            var options = new FileUploadOptions();
            options.fileKey="file";
            //options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.fileName="image.jpeg";
            options.mimeType="image/jpeg";
            options.httpMethod="PUT";
            options.trustAllHosts=true;
            options.chunkedMode=false;


             var header ={};
            //header.ContentType="image/jpeg";
            header.Connection="close";  
            //header.Authorization="Basic c2FoaWwuc2V0aGk6V2VsY29tZUAwNw==";
            options.headers =header;

            var ft = new FileTransfer();                               
            ft.upload(imageURI,encodeURI("servername.com"/profiles/photo.do?
            key="hrhgfhjf23435"),win,fail,options);

            function win(r) {
                alert("file uploaded");
                console.log("Code = " + r.responseCode);
            }

            function fail(error) {
                alert("An error has occurred: Code = " + error.code);
                alert("upload error source " + error.source);
                alert("upload error target " + error.target);
            }
        }
        function onFail(message) {
            //Uncomment to view the image file URI
            alert("in fail");
            alert(message);
        }

On implementation it is returning Code 1 error. I have run this in postman to check the URL but in postman it is working fine.

The imageURI is presumably "file:///data/data/..." - so the issue may be, that the folder is not accessible by FileTransfer.

I had a similar problem, when trying to upload a photo from the photolibrary to social media. So i copied the file to the app's cache and uploaded the picture from there:

 moveFile($scope.imageURI); function moveFile(fileUri) { window.resolveLocalFileSystemURL( fileUri, function(fileEntry){ window.resolveLocalFileSystemURL( cordova.file.externalCacheDirectory, function(dirEntry) { fileEntry.copyTo(dirEntry, fileEntry.name, onSuccess(fileEntry, dirEntry), onFail); }, onFail); }, onFail); } function onSuccess(fileEntry, dirEntry) { $scope.imageURI = dirEntry.nativeURL + fileEntry.name; } function onFail() { alert("error copying picture"); } 

Try this code. It working for me. You need to just call getImage function on button click or DeviceReady wherever you want to call.

function getImage() {
    // Retrieve image file location from specified source
    navigator.camera.getPicture(uploadPhoto, function(message) {
    alert('get picture failed');
},{
    quality: 50, 
    destinationType: navigator.camera.DestinationType.FILE_URI,
    sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
}
    );

}

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

    var params = new Object();
    params.value1 = "test";
    params.value2 = "param";

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

    var ft = new FileTransfer();
    //YOUR_URL = Actual web url where web-service is exists.
    ft.upload(imageURI, "YOUR_URL", win, fail, options);
}

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

function fail(error) {
    alert("An error has occurred: Code = " + error.code);
}

Here is a my PHP webservice that will store my file to the server.

<?php
    print_r($_FILES);
    $new_image_name = "namethisimage.png";
    move_uploaded_file($_FILES["file"]["tmp_name"], "YOUR_SERVER_URL".$new_image_name);
?>

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