This is a migrated thread and some comments may be shown as answers.

Error with uploading image through file transfer

1 Answer 245 Views
Apache Cordova
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Rob
Top achievements
Rank 1
Rob asked on 17 Nov 2014, 10:36 AM
Hi there -

I have tried just about every way I can find in the documentation and in these forums to send an image that I have just taken with my device camara. 

I get my users photo with the following code:

navigator.camera.getPicture(onSuccess, onFail, {
    quality: 100,
    targetWidth : 500,
    destinationType: Camera.DestinationType.FILE_URI,
    sourceType: Camera.PictureSourceType.CAMARA
});
 
function onSuccess(imageData) {
     
    // hide camara initate button and replace with image from camara
    var image = document.getElementById('otherphoto');
    var other = document.getElementById('nophoto');
    image.src = imageData;
    image.style.display = 'block';
    other.style.display = 'none';
     
    // Set the image from camara to my global image object
    theImage = new Object();
    theImage = imageData;
     
    // resolve filename and set global filename variable
    window.resolveLocalFileSystemURL(imageData, function(fileEntry){
         fileName = fileEntry.name;
    }, function(error){
     console.log('about to resolve this files errors');
        console.log(error.code);
    });
 
}
  
function onFail(message) {
    alert('Failed because: ' + message);
}

I then wait for a success callback of a confirmation dialog and trigger the following:

//set upload options
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileName.substr(fileName.lastIndexOf('/')+1);;
options.mimeType = "image/jpeg";
 
options.params = {
    text : text
}
 
options.headers = {
    'Authorization': token
}
     
function win(r) {
    console.log("Code = " + r.responseCode.toString() + "\n");
    console.log("Response = " + r.response.toString() + "\n");
    console.log("Sent = " + r.bytesSent.toString() + "\n");
}
 
var fail = function(error) {
    // error.code == FileTransferError.ABORT_ERR
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
}
 
var ft = new FileTransfer();
ft.upload(theImage, encodeURI(anchorUrl + 'items'), win, fail, options);

Every time server responds with:  An error has occured: Code = 1.

I have tried selecting an image from the photo library to see if that makes any difference but it did not. Also to note my file path of my image from camara is "file:///storage/emulated/0/Android/data/me.anchorman.anchor/cache/1416220390031.jpg". Is this correct?

Any help would be much appreciated.

Thanks.

1 Answer, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 20 Nov 2014, 09:45 AM
Hi Rob,

I was not able to reproduce the issue on my side. I attached a sample project I prepared based on the  code snippets you posted. I used a sample Telerik Backend Services project to store the uploaded image and I have no issues uploading it while testing the app on an Android device.

Can you please run the project on your side and see if it works for you. Let me know how that goes.

Regards,
Tina Stancheva
Telerik
 

Visit the Telerik Verified Plugins Marketplace and get the custom Cordova plugin you need, already tweaked to work seamlessly with AppBuilder.

 
Tags
Apache Cordova
Asked by
Rob
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Share this question
or