解决了困扰一周的问题2
/// <summary> /// 根据对应的imgid 把base=>二进制=>存入数据库 /// </summary> /// <param name="imgID"></param> /// <param name="imgbase"></param> /// <returns></returns> public ActionResult getbase(string imgID,string imgbase) { cb.imageID = Convert.ToInt32(imgID); Encoding.Default.GetBytes(imgbase); byte[] imgbyte = System.Convert.FromBase64String(imgbase); //第一种可以这样 //System.IO.File.WriteAllBytes(@"E:\test\1.jpg", imgbyte); cb.base64 = imgbyte; //第二种 //MemoryStream ms = new MemoryStream(imgbyte); //FileStream fs = new FileStream(@"E:\test\1.jpg", FileMode.Create); //ms.WriteTo(fs); //ms.Close(); //fs.Close(); int result = cm.clipImg(cb); if (result > 0) { ViewData["msg"] = "成功"; } else { ViewData["msg"] = "失败"; } return View(); } /// <summary> /// 根据id 得到图片 /// </summary> /// <param name="imgID"></param> /// <returns></returns> public FileResult getimg(string imgID) { int imageID = 8; ClipBase64 cate = cm.getimg(imageID); if (cate != null) { return File(cate.base64, "image/jpg", "Imag1"); } else { return null; } }
DAL层:
/// <summary> /// 裁剪图片插入数据库 /// </summary> /// <param name="cb"></param> /// <returns></returns> public int clipImg(ClipBase64 cb) { string sql = "INSERT INTO ClipBase64(ImageId,base64)values(@imageID,@base64)"; SqlParameter[] pars = new SqlParameter[2]; pars[0] = new SqlParameter("@imageID", cb.imageID); pars[1] = new SqlParameter("@base64", cb.base64); int result = SqlHelper.ExecuteNoQuery(sql, CommandType.Text, pars); return result; } /// <summary> /// 得到图片 /// </summary> /// <returns></returns> public ClipBase64 getimg(int id) { string sql = "select base64 from ClipBase64 where ImageId=@id" ; SqlParameter[] par = new SqlParameter[1]; par[0] = new SqlParameter("@Id", id); DataTable dt = SqlHelper.ExecuteDataTable(sql, CommandType.Text, par); DataRow dr = dt.Rows[0]; ClipBase64 c = new ClipBase64(); //c.imageID = Convert.ToInt32(dr["AdminId"]); c.base64 = (byte[])(dr["base64"]); return c; }