Click here to Skip to main content
15,891,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
im trying to save a image to server from canvas, when i press the upload button it just resets the canvas.im using two buttons 1 to save the canvas to a hidden field, 2 to upload the canvas.

javascipt save :

saveButton.addEventListener("click", function (event) {
if (signaturePad.isEmpty()) {
alert("Please provide signature first.");
} else {
var signatureData = signaturePad.toDataURL();
document.getElementById("hdnfld").value = signatureData;
alert("signature Saved!");
}
return false;
});
Javascript upload:

<script type="text/javascript">
function UploadPic() {

// generate the image data


var Pic;
Pic.toDataURL("image/png") = document.getElementById('hdnfld').value;
Pic = Pic.replace(/^data:image\/(png|jpg);base64,/, "")

// Sending the image data to Server
$.ajax({
type: 'POST',
url: 'Signature.aspx/UploadPic',
data: '{ "imageData" : "' + Pic + '" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert("Done, Picture Uploaded.");
}


});
}
</script>
Save Button:

Save
upload Button:

<button onclick="javascript:UploadPic();return false;">Upload Picture to Server</button>
c# code:

public void UploadImage(string imageData)
{
string Pic_Path = HttpContext.Current.Server.MapPath("Signature.png");
using (FileStream fs = new FileStream(Pic_Path, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
}
Posted
Comments
ZurdoDev 20-Jun-14 7:56am    
What's the question?

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