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

I'm using a ui control to capture a signature of a customer. The control returns a BASE64 string and since I need to add the image to a document (word mail merge) I need to save the signature first.

My current code works but since I'm displaying the signature control in a modal I have to prevent the page from refreshing itself in order to be able to add another signature or modify some other fields which are passed to the mail merge fields.

I noticed that the page only gets refreshed when I actually save the file and not because of the Ajax POST action in general.

This is what I have so far:

Controller:

C#
[HttpPost]
public JsonResult SaveImage(string imageString)
{

    string base64 = imageString.Substring(imageString.IndexOf(',') + 1);

    base64 = base64.Trim('\0');
    byte[] img = Convert.FromBase64String(base64);

    Image image;

    using (MemoryStream ms = new MemoryStream(img))
    {
        image = Image.FromStream(ms);

        image.Save(Server.MapPath("~/Template/Contract/lgeber.png"), System.Drawing.Imaging.ImageFormat.Png);
    }

    return Json("OK");
}


As soon as image.Save is executed the page refreshes.

Jquery:
$('#saveSignLgeber').on("click", function () {
    var signLgeber = $("#signLgeber").ejSignature("instance");
    lgeberImageString = signLgeber.storeSnap[signLgeber.storeSnap.length - 1]

    $.ajax({
        url: "/Doc/SaveImage",

        type: "POST",
        data: JSON.stringify({ imageString: lgeberImageString }),
        processData: false,
        contentType: 'application/json'
    });

    $('#signLgeberModal').modal('hide');
    $('#signLnehmerModal').modal('show');
});


What I have tried:

I tried to add a
<form>
to my code and use
function(e){ 
    e.preventDefault() 
}


I also tried to use a onclick=return false; to the button and set button type to button.
Nothing works. It still refreshes the page all time.

Any idea?
Posted
Updated 14-Dec-17 5:50am

1 solution

The correct syntax is

$('#saveSignLgeber').on("click", function (e) {
    e.preventDefault();
    .... rest of code ...


if that doesn't work the issue will be with something elsewhere on the page causing the refresh.
 
Share this answer
 
Comments
Member 13261884 15-Dec-17 2:20am    
Thanks for your answer. This is the syntax I have tried but it seems like as soon as the file gets saved it just refreshes no matter what I do.
You don't happen to know if this is the normal behaviour?

I have other functions where this particular syntax works but in this case it just doesn't....

Edit:

I just noticed it is only Chrome which refreshes the page. Firefox doesn't :|

Edit2:

Stupid me. Enabled BrowserLink caused the refresh...

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