0
votes

I have used cordova(navigator.camera.getPicture) to capture image from device. I converted the fileURI into base64 using file reader. But, when i assign the base64 url as img src whereas If i pass the same string to HTTP adapter(Worklight), I saw the encoded data truncated. Please help.

Thanks in advance.

Source Code:

    function tryToSend(evt) {
        encoding = evt.target.result;
        console.log("Encoded File: "+encoding);
        Ext.ComponentQuery.query('#encodedImage')[0].setHtml('<img style="height: 100px; width: 100px;" src="'+encoding+'" />');
        Ext.ComponentQuery.query('#encodedImage')[0].setHidden(false);
    }
    function win(file) {
        alert("FileName:"+file.name + ' & Type:' + file.type);
        selectedFileName = file.name;
        Ext.ComponentQuery.query('#originalImage')[0].setHtml('<img style="height: 100px; width: 100px;" src="'+file.fullPath+'" />');
        Ext.ComponentQuery.query('#originalImage')[0].setHidden(false);
        var reader = new FileReader();
        reader.onloadend = tryToSend;
        var encoded = reader.readAsDataURL(file); 
    }
    function fail(error) {
        console.log(error);
    }
    function onResolveSuccessCompleted(fileEntry) {
        fileEntry.file(win, fail);
    }

    function onResolveFailed(error) {
        console.log(error);
    }
//Call on click of take pic button
function capPic(){
 navigator.camera.getPicture(onCapturePhoto, onFail, {
        quality: 50,
        destinationType: destinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.CAMERA,
        mediaType: navigator.camera.MediaType.ALLMEDIA,
    });
}
//Success
function onCapturePhoto(fileURI) {
    window.resolveLocalFileSystemURI(fileURI, onResolveSuccessCompleted, onResolveFailed);
    fileDetails.push({
        base64ImageData:encoding,
        fileName: selectedFileName,
    });
   alert("File Selected. Please Upload Now");
}

//Sending fileDetails array to HTTP adapter as parameter
var invocationData = {
            adapter : 'SAMPLE_ADAPTER',
            procedure : 'uploadFileNow',
            parameters : [fileDetails]
    };  
    WL.Client.invokeProcedure(invocationData, {
        onSuccess : fileUploadOK,
        onFailure : fileUploadFail,
    });

1) In Logcat, encoding in tryToSend Fn prints completely whereas the next line console.log gives truncated code

//Ajax call Ext.Ajax.request({ url: url, method:'POST', params:fileDetails, success: function(response){ console.log(response); }, failure:function(response){ console.log(response); } });

1
Why would you place the base64 string as the value for the img src? and why do you then mention adapter? and how about to provide some code or better explain your question? - Idan Adar
I just want to check whether it is encoded properly,So assigned that encoded code to img src and it was good but If i pass the same to HTTP adapter,its get truncated - karthik
How do you convert it and send it? Add the code to your image. Is it a GET request? - Idan Adar
Code added. Please check - karthik
This code does not show how you send it. Look at my question again please. - Idan Adar

1 Answers

1
votes

In my logcat the console.log can only print about 4k characters one time. So try to compare the encoded url's length to check if it's really be truncated.