net core base64上传图片

前台

html:

 
  1. <div>
  2.     <input type="file" onchange="selectImage(this.files)" accept="">
  3. </div>

js:

 
  1. function selectImage(files) {
  2.     let reader = new FileReader();
  3.     let file = files[0];
  4.     reader.readAsDataURL(file);
  5.     reader.onload = function () {
  6.         $.post('/Home/UpLoadFile', { filecontent: reader.result }, function (result) {
  7.         });
  8.     }
  9. }

后端

base64转化的工具类:

 
  1. public class Base64Convert
  2.     {
  3.         /// <summary>
  4.         /// 文件转换成Base64字符串
  5.         /// </summary>
  6.         /// <param name="fileName">文件绝对路径</param>
  7.         /// <returns></returns>
  8.         public static String FileToBase64(string fileName)
  9.         {
  10.             string strRet = null;
  11.             try
  12.             {
  13.                 FileStream fs = new FileStream(fileName, FileMode.Open);
  14.                 byte[] bt = new byte[fs.Length];
  15.                 fs.Read(bt, 0, bt.Length);
  16.                 strRet = Convert.ToBase64String(bt);
  17.                 fs.Close();
  18.             }
  19.             catch (Exception ex)
  20.             {
  21.                 throw ex;
  22.             }
  23.             return strRet;
  24.         }
  25.         /// <summary>
  26.         /// Base64字符串转换成文件
  27.         /// </summary>
  28.         /// <param name="strInput">base64字符串</param>
  29.         /// <param name="fileName">保存文件的绝对路径</param>
  30.         /// <returns></returns>
  31.         public static bool Base64ToFileAndSave(string strInput, string fileName)
  32.         {
  33.             bool bTrue = false;
  34.             try
  35.             {
  36.                 byte[] buffer = Convert.FromBase64String(strInput);
  37.                 FileStream fs = new FileStream(fileName, FileMode.CreateNew);
  38.                 fs.Write(buffer, 0, buffer.Length);
  39.                 fs.Close();
  40.                 bTrue = true;
  41.             }
  42.             catch (Exception ex)
  43.             {
  44.                 throw ex;
  45.             }
  46.             return bTrue;
  47.         }
  48.     }

提交的控制器:

 
  1. public IActionResult UpLoadFile()
  2. {
  3.     string base64 = Request.Form["filecontent"];
  4.     //base64需要把前面的申明去掉
  5.     string newbase = base64.Replace("data:image/jpeg;base64,", "");
  6.     //把图片存储到c盘,具体操作的时候位置可以自行改变
  7.     Base64Convert.Base64ToFileAndSave(newbase, "c://timg.jpg");
  8.     return View();
  9. }

优化一下

前台的时候把图片后缀名传过去

 
  1. <script>
  2.     function selectImage(files) {
  3.         let reader = new FileReader();
  4.         let file = files[0];
  5.         console.log(file);
  6.         //取出来文件的后缀名(方便在后台保存的时候用)
  7.         var index = file.name.lastIndexOf(".");
  8.         var fileExt = file.name.substr(index + 1);
  9.         console.log(fileExt);
  10.         reader.readAsDataURL(file);
  11.         reader.onload = function () {
  12.             console.log(reader.result);
  13.             $.post('/Home/UpLoadFile', { filecontent: reader.result, fileExt: fileExt }, function (result) {
  14.             });
  15.         }
  16.     }
  17. </script>

后台去掉前缀的方法换成正则表达式,因为格式不固定

 
  1. /// <summary>
  2. /// base64图片上传
  3. /// </summary>
  4. public void UpLoadFile()
  5. {
  6.     string base64 = Request.Form["filecontent"];
  7.     string fileExt = Request.Form["fileExt"];
  8.     #region base64需要把前面的申明去掉(当然前缀这个也可以放在前端处理)
  9.     //string newbase = base64.Replace("data:image/jpeg;base64,", "");
  10.     //这里用了一下正则表达式因为可能是格式data:image/jpeg;base64,data:image/png;base64等等
  11.     string base64img = Regex.Replace(base64, "data:image/.*;base64,", "");
  12.     #endregion
  13. }

保存的时候图片名字用guid

 
  1. //把图片存储到c盘,具体操作的时候位置可以自行改变
  2. Base64Convert.Base64ToFileAndSave(base64img, "c://" + Guid.NewGuid().ToString().Replace("-", "") +"."+ fileExt);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值