Question

I have following two methods that uploads an image to a remote servlet. For some reason the second parameter which is the arraybuffer is not written to the post request and I am trying to figure out why this is happening. Could some one assist me on this.

setupBinaryMessage = function(metadata) {

    log(metadata);
    var msglen = metadata.length;
    var localcanvas =document.getElementById("image");
    var fullBuffer;
    var myArray;
    if(localcanvas){
        var localcontext = localcanvas.getContext('2d');
    //FOLLOWING 2 LINE OF CODE CONVERTS THE IMAGEDATA TO BINARY
        var imagedata = localcontext.getImageData(0, 0, localcanvas.width, localcanvas.height);
        var canvaspixelarray = imagedata.data;              
        var canvaspixellen = canvaspixelarray.length;               
        var msghead= msglen+"";
        var fbuflen = msglen +canvaspixellen+msghead.length;
        myArray = new ArrayBuffer(fbuflen);
        fullBuffer = new Uint8Array(myArray);
        for (var i=0; i< msghead.length; i++) {
            fullBuffer[i] = msghead.charCodeAt(i);
        }
        for (var i=msglen+msghead.length;i<fbuflen;i++) {
            fullBuffer[i] = canvaspixelarray[count];
            count++;
        };
        return  myArray;        
    } else 
        return null;

};

upladlImageWithPost= function() {
var message =JSON.stringify(this.data); 
var fullBuffer = this.setupBinaryMessage(message);
var formdata = {command : "post", imagedata : fullBuffer,};
alert(jQuery.isPlainObject( formdata ));
var imgPostRequest = $.post( "http://localhost:8080/RestClient/RestClientPOST",fullBuffer, function(response) {     
    response = response.trim();
    console.log(response);
    if(response == "SERVER_READY"){
        alert(response);
        try {

        }catch (error) {
            alert("Web Socket Error "+error.message);
        }
    } else {
        alert("SERVER ERROR");
    }
}.bind(this))
Was it helpful?

Solution

Alright After some help from a GURU I figured the issue. Apparently ARRAYBUFFER is obsolete and real solution is to post the unisinged buffer as it is. But even for that I need to set the AJAX response type to ARRAYBUFFER and then not use JQuery $.post but use original pure XHTTPRequest.

Source

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top