Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to pass an array images though ajax post to a WebMethod. I have written the following code, but I am getting error in that.

Please help me in this.
JavaScript
var imageArray= GetImage();

$.ajax({
            type: "POST",
            url: "Default.aspx/Save",
            data: "{htmlDiv:'" + htmlDiv + "',postData:'" + postData + "',ImageArray:'" + imageArray+ "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                 alert('success');
            },
            error: function (ex) { return true; }
      });

function GetImage() {
    var canvas = document.getElementById("canvasTest");
    var context = canvas .getContext("2d");

    //return { index: 0, image: canvas .toDataURL() };
    var imageArray= [];

    imageArray[imageArray.length] = canvas .toDataURL()

    return canvas ;
}

Thanks,
Posted
v2

There are few problems in the code. First correct them.

1. function GetImage() should return imageArray instead of canvas.

2. Use array like this.
JavaScript
var imageArray = new Array();


3. imageArray[imageArray.length] - What is the imageArray.length ???

4.
JavaScript
var context = canvas .getContext("2d");
imageArray[imageArray.length] = canvas .toDataURL()

Extra spaces are there after the selectors.
it should be
JavaScript
var context = canvas.getContext("2d");
imageArray[imageArray.length] = canvas.toDataURL()


5. context is declared, but not used.

6. htmlDiv, postData not defined.
 
Share this answer
 
Hi ,

I have resolved the issue of sending the image array to ajax post by converting the image to string using JSON.stringify the only problem I am having is to parse the image at server side in vb net.

Please suggest.

Thanks,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900