Question

Im having trouble with sending a message to a WebMethod

The piece is in a ascx so the URL is the parent page or else there is only a 404

i have the JQ code

$("#saveCanvas").click(function () {
        var image = document.getElementById("SolutionDisplay").toDataURL("image/png");
        image = image.replace('data:image/png;base64,', '');

        $.ajax({
            type: 'POST',
            url: "Student.aspx/UploadImage",
            data: '{ "imageData" : "' + image + '" }',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (msg) {
                alert('Image saved successfully !');
            }
        });
});

called from the button

<input type="button" id="saveCanvas" name="saveCanvas" value="Save Image" />

but i keep getting 500 errors, like this

POST http://localhost:83/Student.aspx/UploadImage 500 (Internal Server Error) jquery-1.10.2.js:8720
    send                                jquery-1.10.2.js:8720
    jQuery.extend.ajax                  jquery-1.10.2.js:8150
    (anonymous function)                Student.aspx?Page=Assignment&&CourseID=14:709
    jQuery.event.dispatch               jquery-1.10.2.js:5109
    elemData.handle                     jquery-1.10.2.js:4780

Could it have something to do with the GET url being?

http://localhost:83/Student.aspx?Page=Assignment&&CourseID=14

the server code should be straightforward

on the class there is a

[ScriptService]

and in the class there is the "handler"

static string PathTest = @"D:\";
[WebMethod()]
public static void UploadImage(string imageData)
{
    string fileNameWitPath = PathTest + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
    using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
    {
        using (BinaryWriter bw = new BinaryWriter(fs))
        {
            byte[] data = Convert.FromBase64String(imageData);
            bw.Write(data);
            bw.Close();
        }
    }
}
Was it helpful?

Solution

I solved it. the problem was running the ajax under a usercontroll. moved the methods out to the parent aspx and it ran beautifully.

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