示例#1
0
        /// <summary>
        ///  获取WebConfig.images下的某个子节点
        /// </summary>
        /// <param name="rules">允许上传图片集合</param>
        /// <param name="ChildConfigName">节点名</param>
        /// <returns></returns>
        public ImageConfigInfo GetImageConfigInfo(ref string rules, string configName = "product")
        {
            try
            {
                var             imagesSection   = ConfigurationManager.GetSection("images") as XmlNode;
                ImageConfigInfo imageConfigInfo = new ImageConfigInfo();
                rules = imagesSection.Attributes["rules"].Value;

                List <SaveConfigInfo> saveConfigInfoList = new List <SaveConfigInfo>();
                XmlNode gradesNode = imagesSection.SelectSingleNode(configName + "image");
                for (int i = 0; i < gradesNode.Attributes.Count; i++)
                {
                    string name  = gradesNode.Attributes[i].Name;
                    string value = gradesNode.Attributes[name].Value;
                    switch (name)
                    {
                    case "savepath":
                        imageConfigInfo.SavePath = value; break;

                    case "filename":
                        imageConfigInfo.FileName = value; break;

                    case "url":
                        imageConfigInfo.URL = value; break;

                    case "size":
                        imageConfigInfo.Size = int.Parse(value); break;

                    case "watermark":
                        imageConfigInfo.Watermark = value; break;
                    }
                }
                XmlNodeList childList = gradesNode.ChildNodes;
                for (int i = 0; i < childList.Count; i++)
                {
                    SaveConfigInfo saveConfigInfo = new SaveConfigInfo();
                    saveConfigInfo.Key    = childList[i].Attributes["key"].Value;
                    saveConfigInfo.Size   = int.Parse(childList[i].Attributes["size"].Value);
                    saveConfigInfo.Suffix = childList[i].Attributes["suffix"].Value;
                    saveConfigInfoList.Add(saveConfigInfo);
                }
                imageConfigInfo.SaveConfig = saveConfigInfoList;
                return(imageConfigInfo);
            }
            catch
            {
                return(null);
            }
        }
示例#2
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);
        }