Search code examples
c#asp.net-mvcimagebase64memorystream

Refresh Web Page after Converting base64 to image


I have a base64 data and I want to convert it to Image in Asp.net. I use this Code

byte[] data = Convert.FromBase64String(imageData);
var path = Server.MapPath("~/ImageFolder/") +  "AAA.jpeg";
System.IO.File.WriteAllBytes(path, data); //(*)

when the code reach at line (*), Page Refresh, but I don't want to refresh. what is solution for converting base64 to image and save in folder without page refreshing?

if i use MemoryStream, the Problem exist and page will refresh.

UPDATE

I have this codes on client sides

function ajaxCall(url, data, callBackFunction) {
    $.ajax({
        url: url,
        type: "POST",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        data: data,
        success: function (response) {
            callBackFunction(response);
        },
        error: function (xhr, status, error) {
            alert(error);
        },
    });
}

And

    ajaxCall('/Default/Save/', {
 final: _final, // Some boolean flag
        imageData: imgData, // Image Base64
    }, function (response) {
        $('#resultDiv').html(response)
    });

And in Server

[HttpPost]
    public ActionResult Save(bool final,string imageData)
    {
        var thumbHash = "";
        if (imageData.Length > 0)
        {
            try
            {
                imageData = imageData.Replace("data:image/jpeg;base64,", String.Empty);
                byte[] data = Convert.FromBase64String(imageData);
                thumbHash = "123456789ABC";
   var path = Server.MapPath("~/ImageFolder/") +  "AAA.jpeg";
              
                System.IO.File.WriteAllBytes(path, data); //(*)

            }
            catch (Exception ex)
            {
                thumbHash = "";
            }
        }

      
        if (final)
            return RedirectToAction("AddOne", "Cart", new { id = 12 });

      
        return Json( "Ahm" );
    }

Solution

  • Problem Solved

    Code has NOT any problem. after checking a lot, I found the Solution. I only Uncheck 'Enable Browser Link' in Visual Studio and Problem Solved. Thanks from Everybody Who Participate in this Question.