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.
6 Replies
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX6mo ago
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:
private readonly HttpClient _httpClient;

private static async Task UploadSampleFileAsync()
{
await using var stream = File.OpenRead("./Test.txt");
using var request = new HttpRequestMessage(HttpMethod.Post, "/api/whatever/upload");
using var content = new MultipartFormDataContent
{
{ new StreamContent(stream), "file", "Test.txt" }
};

request.Content = content;

await client.SendAsync(request);
}
private readonly HttpClient _httpClient;

private static async Task UploadSampleFileAsync()
{
await using var stream = File.OpenRead("./Test.txt");
using var request = new HttpRequestMessage(HttpMethod.Post, "/api/whatever/upload");
using var content = new MultipartFormDataContent
{
{ new StreamContent(stream), "file", "Test.txt" }
};

request.Content = content;

await client.SendAsync(request);
}
ServerCode:
[ApiController]
[Route("api/whatever")]
public class FileController : ControllerBase
{
[HttpPost("upload")]
public IActionResult Upload([FromForm] IFormFile file)
{
// code responsible for file processing
return Ok();
}
}
[ApiController]
[Route("api/whatever")]
public class FileController : ControllerBase
{
[HttpPost("upload")]
public IActionResult Upload([FromForm] IFormFile file)
{
// code responsible for file processing
return Ok();
}
}
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.
abdurrahim bsrn
abdurrahim bsrnOP6mo ago
I am using asp.net web applicatin (framework 4.5). So this solution can't help me : (
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
abdurrahim bsrn
abdurrahim bsrnOP6mo ago
you are right, its my fault : )
danyule
danyule6mo ago
When you say you cannot transfer data, what do you mean?
Want results from more Discord servers?
Add your server