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

  • A+
所属分类:.NET技术
摘要

前端是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) {

前端是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);
}

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