zoukankan      html  css  js  c++  java
  • 解决了困扰一周的问题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:	est1.jpg", imgbyte);
                cb.base64 = imgbyte;
                //第二种
                //MemoryStream ms = new MemoryStream(imgbyte);
                //FileStream fs = new FileStream(@"E:	est1.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;
            }

  • 相关阅读:
    java对象的四种引用
    linux安装python3
    ORACLE配置重做日志文件
    oracle添加控制文件,ORA-00214: 错误
    oracle new 和old 关键字
    with open
    json库
    requests
    urllib模块
    python读取txt天气数据并使用matplotlib模块绘图
  • 原文地址:https://www.cnblogs.com/MartinLee/p/7622724.html
Copyright © 2011-2022 走看看