问题描述
我有一个 base64 数据,我想在 Asp.net 中将其转换为 Image。我用这个代码
byte[] data = Convert.FromBase64String(imageData);
var path = Server.MapPath("~/ImageFolder/") + "AAA.jpeg";
System.IO.File.WriteallBytes(path,data); //(*)
当代码到达第 (*) 行时,页面刷新,但我不想刷新。 什么是base64转图片并保存在文件夹中而不刷新页面的解决方案?
如果我使用 MemoryStream
,问题存在并且页面将刷新。
更新
我在客户端有这个代码
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);
},});
}
和
ajaxCall('/Default/Save/',{
final: _final,// Some boolean flag
imageData: imgData,// Image Base64
},function (response) {
$('#resultDiv').html(response)
});
在服务器中
[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" );
}
解决方法
问题已解决
代码没有任何问题。检查了很多之后,我找到了解决方案。 我只在 Visual Studio 和问题解决中取消选中“启用浏览器链接”。 感谢所有参与此问题的人。