Beispiel #1
0
        /// <summary>
        ///  根据UpLoadImageInfo.Type返回文件名
        /// </summary>
        /// <param name="upLoadImage">UpLoadImageInfo</param>
        /// <param name="extName">后缀名</param>
        /// <param name="ibyte">没有名字的情况下</param>
        /// <param name="filePath">ref 文件路径</param>
        /// <returns>图片名</returns>
        private string FileNameStrategy(UpLoadImageInfo upLoadImageInfo, string suffix, byte[] ibyte, ref string filePath)
        {
            string imageFileName = string.Empty;
            string index         = string.Empty;

            if (upLoadImageInfo.Index != 0)
            {
                index = upLoadImageInfo.Index.ToString();
            }
            if (string.IsNullOrEmpty(upLoadImageInfo.FileName) == false && upLoadImageInfo.FileName != "")
            {
                //已经有文件名
                if (upLoadImageInfo.FileName.IndexOf(".") != -1)
                {
                    //default.jpg完整名
                    string[] splitFileName = upLoadImageInfo.FileName.Split('.');
                    imageFileName = string.Format("{0}{1}{2}.{3}", splitFileName[0], suffix, index, splitFileName[1]);
                }
                else
                {
                    //例:default无完整名
                    imageFileName = string.Format("{0}{1}{2}.{3}", upLoadImageInfo.FileName, suffix, index, upLoadImageInfo.ExtName);
                }
            }
            else
            {
                //没有文件名时
                MD5    md5      = new MD5CryptoServiceProvider();
                byte[] fileKey  = md5.ComputeHash(ibyte);
                string md5Ibyte = Convert.ToBase64String(fileKey);
                md5Ibyte = md5Ibyte.Replace('/', 'f');
                md5Ibyte = md5Ibyte.Replace('@', 'w');
                md5Ibyte = md5Ibyte.Replace('+', 'y');
                md5Ibyte = md5Ibyte.Replace(' ', 'j');
                md5Ibyte = md5Ibyte.Replace('.', 'x');
                md5Ibyte = md5Ibyte.Replace('\\', 'd');
                md5Ibyte = md5Ibyte.Substring(0, 19);
                filePath = md5Ibyte.Substring(0, 4) + "/" +
                           md5Ibyte.Substring(4, 3) + "/" +
                           md5Ibyte.Substring(7, 4);
                imageFileName = md5Ibyte.Substring(11) + suffix + "." + upLoadImageInfo.ExtName;
            }
            return(imageFileName);
        }
Beispiel #2
0
        /// <summary>
        /// 上传图片  图片作byse64字符串流传递
        /// </summary>
        public JsonResultObject UpLoadImage()
        {
            myLog.InfoFormat("开始");
            var             result          = new JsonResultObject();
            UpLoadImageInfo upLoadImageInfo = new UpLoadImageInfo();

            upLoadImageInfo.ImageByte  = HttpContext.Current.Request["imageByte"];
            upLoadImageInfo.SourceType = HttpContext.Current.Request["sourceType"];
            upLoadImageInfo.ExtName    = HttpContext.Current.Request["extName"];
            upLoadImageInfo.Type       = HttpContext.Current.Request["type"];
            upLoadImageInfo.FilePath   = HttpContext.Current.Request["filePath"];
            upLoadImageInfo.FileName   = HttpContext.Current.Request["fileName"];
            upLoadImageInfo.FilePath   = upLoadImageInfo.FilePath == ""? null :upLoadImageInfo.FilePath;
            upLoadImageInfo.FileName   = upLoadImageInfo.FileName == "" ? null : upLoadImageInfo.FileName;
            string index = HttpContext.Current.Request["index"];

            if (index != null && index != "" && index != "0")
            {
                upLoadImageInfo.Index = int.Parse(index);
            }
            if (upLoadImageInfo.Type == null || upLoadImageInfo.Type == "")
            {
                upLoadImageInfo.Type = "other";
            }
            if (string.IsNullOrEmpty(upLoadImageInfo.ImageByte) == false && upLoadImageInfo.ImageByte != "")
            {
                try
                {
                    byte[] ibyte = ImageConvertUtil.FromBase64ToByte(upLoadImageInfo.ImageByte);
                    result = ReturnUpLoadStatus(ibyte, upLoadImageInfo); //存放图片,并返回数据状态
                }
                catch (Exception ex)
                {
                    result.msg = "数据添加异常!";
                    myLog.InfoFormat("图片上传异常 " + ex.Message);
                }
            }
            else
            {
                result.status = false;
                result.msg    = "上传失败,没有解析图片!";
            }
            return(result);
        }
Beispiel #3
0
        private JsonResultObject ReturnUpLoadStatus(byte[] ibyte, UpLoadImageInfo upLoadImage)
        {
            var result = new JsonResultObject();
            //result.data的返回路径集合
            List <object> allOutPath = new List <object>();

            //允许上传图片集合后缀
            string          rules           = string.Empty;
            ImageConfigInfo imageConfigInfo = GetImageConfigInfo(ref rules, upLoadImage.Type);

            if (IsValidPic(ibyte) == false)
            {
                result.msg    = "您上传的不是图片类型";
                result.status = false;
                return(result);
            }
            else if (rules.IndexOf(upLoadImage.ExtName) == -1 || rules.Contains(upLoadImage.ExtName) == false)
            {
                result.msg    = "您上传的不是图片类型";
                result.status = false;
                return(result);
            }
            //二进制图片流会比原有的图片大130%-150%
            double maxSize = imageConfigInfo.Size * 1024 * 1.5;

            if (upLoadImage.ImageByte.Length > maxSize)
            {
                result.msg    = "图片已超过实际大小:" + imageConfigInfo.Size + "KB";
                result.status = false;
                return(result);
            }

            string filePath = string.Empty; //图片路径
            string savePath = string.Empty; //图片保存文件夹位置

            //创建默认原图
            imageConfigInfo.SaveConfig.Add(new SaveConfigInfo {
                Key = "Origin", Size = -1, Suffix = "_O"
            });
            string defaultImagePath = string.Empty;

            for (int i = imageConfigInfo.SaveConfig.Count - 1; i >= 0; i--)
            {
                //图片名称
                string imageFileName = FileNameStrategy(upLoadImage, imageConfigInfo.SaveConfig[i].Suffix, ibyte, ref filePath);
                //创建图片图片目录,判定是否有子文件夹
                if (upLoadImage.FilePath == null && upLoadImage.FileName == null)
                {
                    savePath = imageConfigInfo.SavePath + imageConfigInfo.FileName + "/" + filePath;
                }
                else
                {
                    savePath = imageConfigInfo.SavePath + imageConfigInfo.FileName + "/" + upLoadImage.FilePath;
                }

                if (imageConfigInfo.SaveConfig[i].Suffix == "_L")
                {
                    if (upLoadImage.FilePath == null && upLoadImage.FileName == null)
                    {
                        allOutPath.Add(new { ImagePath = filePath + "/" + imageFileName, Url = imageConfigInfo.URL });
                    }
                    else
                    {
                        allOutPath.Add(new { ImagePath = upLoadImage.FilePath + "/" + imageFileName, Url = imageConfigInfo.URL });
                    }
                }

                Directory.CreateDirectory(savePath);
                bool isOrigin;
                if (imageConfigInfo.SaveConfig[i].Size == -1 || imageConfigInfo.SaveConfig[i].Key == "Origin")
                {
                    isOrigin         = true;
                    defaultImagePath = savePath + "/" + imageFileName;
                    SaveImage(savePath
                              , imageFileName
                              , ibyte
                              , isOrigin
                              , imageConfigInfo.SaveConfig[i].Size
                              , imageConfigInfo.SaveConfig[i].Size
                              , upLoadImage.ExtName);
                }
                else
                {
                    isOrigin = false;
                    string otherImagePath = savePath + "/" + imageFileName;
                    SaveImage(otherImagePath, ibyte, upLoadImage.ExtName, imageConfigInfo.SaveConfig[i].Size, false);
                }
            }

            result.data   = allOutPath;
            result.status = true;
            result.msg    = "图片已上传完成!";
            return(result);
        }