Example #1
0
        /// <summary>
        /// 上传文件-处理base64位图片数据的方法
        /// </summary>
        /// <param name="base64"></param>
        /// <param name="save_path"></param>
        /// <returns></returns>
        public UnifiedResultEntity <string> Upload_For_Base64Data(string base64, string ServerPath)
        {
            UnifiedResultEntity <string> response_entity = new UnifiedResultEntity <string>();

            response_entity.msg = 0;

            if (string.IsNullOrWhiteSpace(base64) || string.IsNullOrWhiteSpace(ServerPath))
            {
                response_entity.msgbox = "缺少参数";
                return(response_entity);
            }

            //保存的绝对路径,不包含文件名
            string filepath_no_name = IOHelper.GetMapPath(ServerPath);

            try
            {
                string guid      = Guid.NewGuid().ToString();
                string saveName  = guid;// +".JPG"; // 保存文件名称
                string file_ext  = "";
                string file_path = IOHelper.Base64ToImgSave(base64, ServerPath, saveName, out file_ext);

                if (string.IsNullOrWhiteSpace(file_path) || string.IsNullOrWhiteSpace(file_ext))
                {
                    response_entity.msgbox = "文件保存失败";
                    return(response_entity);
                }

                saveName = saveName + "." + file_ext;

                //保存后文件的绝对路径,包括文件名
                string filePath = IOHelper.GetMapPath(file_path);

                //获取MD5值
                string md5 = IOHelper.GetMD5HashFromFile(filePath);
                if (System.IO.File.Exists(filepath_no_name + md5 + "." + file_ext))
                {
                    System.IO.File.Delete(filePath); //把刚刚上传的给删掉,只用原有的文件
                }
                else //不存在,改名为md5值保存
                {
                    System.IO.File.Move(filePath, filepath_no_name + md5 + "." + file_ext); //给文件改名
                }
                response_entity.msg    = 1;
                response_entity.msgbox = "上传成功";
                response_entity.data   = ServerPath + md5 + "." + file_ext;
                return(response_entity);
            }
            catch (Exception ex)
            {
                response_entity.msgbox = ex.Message;
                return(response_entity);
            }
        }