前端页面64位图片上传服务器,后台方法:
public string UploadBase64Img() { try { string base64 = Request["imgUrl"]; base64= base64.Substring(base64.IndexOf(",") + 1); //将‘,’以前的多余字符串删除 System.Drawing.Bitmap bitmap = null;//定义一个Bitmap对象,接收转换完成的图片 byte[] byt64 = Convert.FromBase64String(base64);//将纯净资源Base64转换成等效的8位无符号整形数组 MemoryStream ms = new MemoryStream(byt64);//转换成无法调整大小的MemoryStream对象 bitmap = new System.Drawing.Bitmap(ms);//将MemoryStream对象转换成Bitmap对象 ms.Close();//关闭当前流,并释放所有与之关联的资源 // 文件上传后的保存路径 string savePath = Server.MapPath("~/UploadImgs/"); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } DateTime _now = DateTime.Now; var r = new Random(); int n = r.Next(1000, 9999); string imgName = string.Format("{0}-{1}-{2}.png", _now.ToString("yyyy-MM-dd"), _now.Millisecond, n);//图片名称 string imgPath = savePath + imgName; bitmap.Save(imgPath, System.Drawing.Imaging.ImageFormat.Png);//保存到服务器路径 return imgName; } catch (Exception ex) { throw ex; } }