发布Arraybuffer

时间:2013-11-25 12:59:16

标签: javascript html5 arraybuffer

我有以下两种方法将图像上传到远程servlet。由于某种原因,第二个参数是arraybuffer没有写入post请求,我试图弄清楚为什么会发生这种情况。有人可以帮助我。

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))

1 个答案:

答案 0 :(得分:0)

好的,在GURU的帮助下,我想出了这个问题。显然ARRAYBUFFER已经过时,真正的解决方案就是发布unisinged缓冲区。但即便如此,我需要将AJAX响应类型设置为ARRAYBUFFER,然后不使用JQuery $ .post但是 使用原始的纯XHTTPRequest。

Source