abdurrahim bsrn
How can I send image data from javascript to c#
$('#download').click(function () {
var data = signature.jSignature('getData', 'image');
var imageData = "data:" + data[0] + "," + data[1]; // Base64 verisi
// AJAX isteği ile sunucuya gönderin
$.ajax({
type: "POST",
url: "imzaDeneme.aspx/SaveSignature",
data: JSON.stringify({ image: imageData }),
contentType: "application/json; charset=utf-8",
dataType: "json", // Yazım hatasını düzelttim
success: function (myresult) {
console.log('İmza başarıyla kaydedildi.');
},
error: function (xhr, status, error) {
console.error('Bir hata oluştu:', error);
}
});
});
[WebMethod]
public static string SaveSignature(SignatureData data)
{
try
{
string base64 = data.image.Split(',')[1];
byte[] imageBytes = Convert.FromBase64String(base64);
string filePath = HttpContext.Current.Server.MapPath("~/imzalar/signature.png");
File.WriteAllBytes(filePath, imageBytes); return "Success"; } catch (Exception ex) { return "Error: " + ex.Message; } } public class SignatureData { public string image { get; set; } } I did it this way, but I cannot transfer data.
File.WriteAllBytes(filePath, imageBytes); return "Success"; } catch (Exception ex) { return "Error: " + ex.Message; } } public class SignatureData { public string image { get; set; } } I did it this way, but I cannot transfer data.
9 replies