关于前端Ajaxc传FormData后台如何接收转base64

前端是Jquery的ajax,后台是C#MVC,代码如下:

<------前端----->

var formData = new FormData();
formData.append("File", files);
$.ajax({
url: "/Publish/TempDescUpLoad" ,
type: 'POST',
data: formData,
// 告诉jQuery不要去处理发送的数据
processData: false,
// 告诉equerry不要去设置Content-Type请求头
contentType: false,
beforeSend: function () {//发送之前
console.log("正在进行,请稍候");
},
success: function (data) {

}

});

<------后台代码----->

using (MemoryStream memoryStream = new MemoryStream())
{

Image img = Image.FromStream(Request.Files["File"].InputStream);

img.Save(memoryStream, img.RawFormat);
byte[] imageBytes = memoryStream.ToArray();
string base64 = Convert.ToBase64String(imageBytes);

string ImgMapPath = "data:image/jpg;base64," + base64;

return Json(new { msg = "上传成功", value = ImgMapPath, title = "", imgsize = "" }, JsonRequestBehavior.AllowGet);
}

如果有不懂评论留言,对你有用点个赞谢谢!

posted @ 2020-09-04 14:14  陈燃lvs  阅读(766)  评论(0编辑  收藏  举报