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.
6 Replies
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
In case you did not read the docs:
https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0
Client Code:
ServerCode:
The field's name in the body when doing the POST, needs to match the binded field in the Controler's Action
https://media.discordapp.net/attachments/569261465463160900/1001188081069735936/unknown.png
Upload files in ASP.NET Core
How to use model binding and streaming to upload files in ASP.NET Core MVC.
I am using asp.net web applicatin (framework 4.5). So this solution can't help me : (
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
you are right, its my fault : )
When you say you cannot transfer data, what do you mean?