手机APP上传头像保存到oracle数据库中并显示头像(服务端)

来源:互联网 发布:历史发明家袁腾飞 知乎 编辑:程序博客网 时间:2024/05/01 08:13

一、数据库中保存头像的字段类型为:BLOB类型

二、手机APP上点击更改头像,可以选择本地照片或着现场自拍一张,确定之后就访问服务端页面,进行上传到数据库中。这里接收两个参数。

    /// <summary>    /// 头像上传    /// </summary>    public class HeadImageUp : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/plain";            string operdm = "";//用户的operdm            string fileImage = "";//图片的base64编码            string strResult = "";//返回值                      if (!string.IsNullOrEmpty(context.Request.Form.Get("operdm")))                operdm = context.Request.Form.Get("operdm");            if (!string.IsNullOrEmpty(context.Request.Form.Get("image")))                fileImage = context.Request.Form.Get("image");                     if (fileImage != "" && operdm != "")            {                strResult = GetResult(fileImage, operdm);            }            else            {                strResult = "-2";//参数为空            }                        context.Response.Write(strResult);        }        public bool IsReusable        {            get            {                return false;            }        }        /// <summary>        /// 获得返回值        /// </summary>        /// <param name="strImage">头像base64编码</param>        /// <param name="operdm">用户operdm</param>        /// <returns></returns>        public string GetResult(string strImage, string operdm)        {            string strResult = "";            byte[] arr = Convert.FromBase64String(strImage);//将string类型的base64编码转成字节            DBTrans dbTr = new DBTrans();            string strSQL = "update OPER_TB set HEADBASE =:HEADBASE,MODIFYTIME=sysdate where OPERDM='" + operdm + "'";            bool b = dbTr.OperateImageToOracle(arr, strSQL);            if (b)//将头像保存在人员表中            {                strResult = "1";//保存成功            }            else            {                strResult = "-1";//保存失败            }            return strResult;        }    }
        /// <summary>        /// 将头像保存到oracle,或者更新oracle中的图片        /// </summary>        /// <param name="headImage">图片字节</param>        /// <param name="strSql">sql语句</param>        /// <returns></returns>        public Boolean OperateImageToOracle(Byte[] headImage, string strSql)        {            try            {                cnn = new OracleConnection(strCon);                cnn.Open();                OracleParameter image = null;                OracleCommand cmd = null;                cmd = cnn.CreateCommand(); //mPropConnection是OracleConnection类型变量                cmd.CommandText = strSql;                image = new OracleParameter(":HEADBASE", OracleDbType.Blob, headImage.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, headImage);                cmd.Parameters.Add(image);                cmd.ExecuteNonQuery();                cnn.Close();                return true;            }            catch (Exception)            {                return false;            }        }
三、现在可以在数据库中看到上传的图片

四、服务端实现显示头像,只接收operdm就能找到该头像

    /// <summary>    /// 头像下载    /// </summary>    public class HeadImageDown : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/plain";            string operdm = "";//operdm代码            string strBase = "";//返回的64base编码            if (!string.IsNullOrEmpty(context.Request["operdm"]))                operdm = context.Request["operdm"].ToString();            if (operdm != "")            {                DBTrans dbTr = new DBTrans();                strBase = dbTr.getBase(operdm);            }            else            {                strBase = "-2";//参数为空            }            context.Response.Write(strBase);        }        public bool IsReusable        {            get            {                return false;            }        }    }
        /// <summary>        /// 查询数据库中headbase头像,将头像图片转换为64base编码        /// </summary>        /// <param name="operdm">用户代码</param>        /// <returns></returns>        public string getBase(string operdm)        {            string strImg = "";//返回值            cnn = new OracleConnection(strCon);            string sqlStr = "select HEADBASE from oper_tb where operdm='" + operdm + "'";            DataSet ds = ExecuteDataSet(sqlStr);            string strHead = ds.Tables[0].Rows[0]["HEADBASE"].ToString();            //判断是否有图片            if (strHead != "")            {                cnn.Open();//打开数据链接                OracleCommand cmd = new OracleCommand(sqlStr, cnn);                OracleDataReader rs = cmd.ExecuteReader();                while (rs.Read())                {                    byte[] File = (byte[])rs["HEADBASE"];                    using (MemoryStream ms = new MemoryStream(File))                    {                        strImg = Convert.ToBase64String(File);                    }                }                rs.Close();                cnn.Clone();            }            else            {                strImg = "0";//没有头像            }            return strImg;        }
五、手机APP中现在头像的效果







1 0
原创粉丝点击