Exemple #1
0
        //private UploadConfigInfo GetDefaultUploadConfigInfo()
        //{
        //    UploadConfigInfo model = new UploadConfigInfo();
        //    return model;
        //}
        #endregion

        #region 加载上传文件的配置
        private void LoadUploadConfig()
        {
            string sFile = GetUploadConfigFile();

            if (string.IsNullOrEmpty(sFile))
            {
                return;
            }
            var ConfigData = from p in XElement.Load(HttpContext.Current.Server.MapPath(sFile)).Descendants("File")
                             select p;

            foreach (XElement xe in ConfigData)
            {
                UploadConfigInfo model = new UploadConfigInfo();
                model.Key            = xe.Attribute("Key").Value;
                model.URL            = xe.Attribute("URL").Value;
                model.IsImg          = xe.Attribute("IsImg").Value == "true";
                model.FileNamePrefix = xe.Attribute("FileNamePrefix").Value;
                model.FileExt        = xe.Attribute("FileExt").Value;
                model.MaxSize        = xe.Attribute("MaxSize") == null ? 0 : Convert.ToInt32(xe.Attribute("MaxSize").Value);
                model.ToThumbnail    = xe.Attribute("ToThumbnail") == null ? false : (xe.Attribute("ToThumbnail").Value == "true");
                model.Width          = xe.Attribute("Width") == null ? 0 : Convert.ToInt32(xe.Attribute("Width").Value);
                model.Height         = xe.Attribute("Height") == null ? 0 : Convert.ToInt32(xe.Attribute("Height").Value);
                if (xe.Attribute("ThumbnailMode") != null && xe.Attribute("ThumbnailMode").Value != "")
                {
                    model.ThumbnailMode = (ThumbnailMode)Enum.Parse(typeof(ThumbnailMode), xe.Attribute("ThumbnailMode").Value, true);
                }
                if (xe.Attribute("OtherThumbnail") != null)
                {
                    model.OtherThumbnail = xe.Attribute("OtherThumbnail").Value;
                }
                else
                {
                    model.OtherThumbnail = "";
                }
                model.MaxModeSize = xe.Attribute("MaxModeSize") == null ? 0 : Convert.ToInt32(xe.Attribute("MaxModeSize").Value);
                if (xe.Attribute("MaxMode") != null && xe.Attribute("MaxMode").Value != "")
                {
                    model.MaxMode = (ThumbnailMode)Enum.Parse(typeof(ThumbnailMode), xe.Attribute("MaxMode").Value, true);
                }
                model.ToWater = xe.Attribute("ToWater") == null ? false : (xe.Attribute("ToWater").Value == "true");
                if (xe.Attribute("WaterPosition") != null && xe.Attribute("WaterPosition").Value != "")
                {
                    model.WaterPosition = (WaterPositionOptions)Enum.Parse(typeof(WaterPositionOptions), xe.Attribute("WaterPosition").Value, true);
                }
                _List.Add(model);
            }
        }
Exemple #2
0
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="oFile"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public static UploadResultInfo SaveFile(HttpPostedFile oFile, UploadConfigInfo model)
        {
            UploadResultInfo info = new UploadResultInfo();

            /*----------------------------------------------------------------
            *  Key             键值
            *  URL             文件保存路径 {$CityCode}:citycode,{$CompanyID}:企业ID
            *  IsImg           是否是图片   【true/false】
            *  FileNamePrefix  文件名的前缀 如:company******.jpg
            *  FileExt         允许上传的文件后缀 "|"分隔 如:.png|.jpg|.gif|.bmp
            *  MaxSize         允许上传文件大小 单位:KB 0:不受限制
            *  ToThumbnail     是否生成缩略图  【true/false】
            *  Width           缩略图的宽度
            *  Height          缩略图的高度
            *  ThumbnailMode   缩略图方式 【HW,W,H,Cut,Ration,Fill】
            *  MaxModeSize     按照最大尺度(高度或者宽度)生成缩略图  0:表示不按照最大尺度生成缩略图
            *  MaxMode         最大尺度方式(高度或者宽度) 【H/W】
            *  ToWater         是否打水印  【true/false】
            *  WaterPosition   水印位置 【LeftTop,RightTop,Middle,LeftBottom,RightBottom】
            *  ----------------------------------------------------------------*/
            #region 文件限制判断
            if (!CheckFileExt(oFile.FileName, model.FileExt))
            {
                info.Msg          = "文件类型错误,应为后缀为[" + model.FileExt + "]的文件。";
                info.UploadStatus = false;
                return(info);
            }
            if (model.MaxSize != 0 && model.MaxSize < oFile.ContentLength / 1024)
            {
                int size = model.MaxSize / 1024;
                if (size > 1)
                {
                    info.Msg = "文件大小限制为" + (size / 1024) + "MB";
                }
                else
                {
                    info.Msg = "文件大小限制为" + model.MaxSize + "KB";
                }
                info.UploadStatus = false;
                return(info);
            }
            #endregion

            string fileName = GetFileNameByTime(model.FileNamePrefix, Path.GetExtension(oFile.FileName));

            string fileFullName = model.URL + fileName;

            //Response.Write("OK:图片上传成功。-" + fileName);
            //return;

            #region   文件
            if (!model.IsImg)//普通文件
            {
                if (UploadFile(oFile, fileFullName))
                {
                    info.FileName = fileName;
                }
                else
                {
                    info.UploadStatus = false;
                    info.Msg          = "文件上传失败,请重试。";
                }
            }
            else//图片
            {
                info.FileType = UploadFileType.Img;
                #region 图片
                if (model.ToThumbnail && model.ToWater)//缩略图    +   水印
                {
                    SaveOtherThumbnail(oFile, model.OtherThumbnail, model.URL, fileName);
                    if (model.MaxModeSize != 0)//按照最大尺度进行缩略图
                    {
                        if (UploadPicFileClipByMaxSize(oFile, fileFullName, model.MaxModeSize, model.MaxMode, model.WaterPosition, model.Width, model.Height, model.ThumbnailMode))
                        {
                            info.Msg      = "图片上传成功。";
                            info.FileName = fileName;
                        }
                        else
                        {
                            info.UploadStatus = false;
                            info.Msg          = "图片上传失败,请重试。";
                        }
                    }
                    else
                    {
                        if (UploadPicFileToWaterAndThumbnail(oFile, fileFullName, model.WaterPosition, model.Width, model.Height, model.ThumbnailMode))
                        {
                            info.Msg      = "图片上传成功。";
                            info.FileName = fileName;
                        }
                        else
                        {
                            info.UploadStatus = false;
                            info.Msg          = "图片上传失败,请重试。";
                        }
                    }
                }
                else if (model.ToThumbnail)//缩略图
                {
                    SaveOtherThumbnail(oFile, model.OtherThumbnail, model.URL, fileName);
                    if (model.MaxModeSize != 0)//按照最大尺度进行缩略图
                    {
                        if (UploadPicFileClipByMaxSize(oFile, fileFullName, model.MaxModeSize, model.MaxMode, model.Width, model.Height, model.ThumbnailMode))
                        {
                            info.Msg      = "图片上传成功。";
                            info.FileName = fileName;
                        }
                        else
                        {
                            info.UploadStatus = false;
                            info.Msg          = "图片上传失败,请重试。";
                        }
                    }
                    else
                    {
                        if (UploadPicFileToThumbnail(oFile, fileFullName, model.Width, model.Height, model.ThumbnailMode))
                        {
                            info.Msg      = "图片上传成功。";
                            info.FileName = fileName;
                        }
                        else
                        {
                            info.UploadStatus = false;
                            info.Msg          = "图片上传失败,请重试。";
                        }
                    }
                }
                else if (model.ToWater)//水印
                {
                    if (UploadPicFileToWater(oFile, fileFullName, model.WaterPosition))
                    {
                        info.Msg      = "图片上传成功。";
                        info.FileName = fileName;
                    }
                    else
                    {
                        info.UploadStatus = false;
                        info.Msg          = "图片上传失败,请重试。";
                    }
                }
                else
                {
                    //Image temImg = Image.FromStream(oFile.InputStream);
                    //if (temImg.Height == model.Height && temImg.Width == model.Width)
                    //{
                    //    SaveOtherThumbnail(oFile, model.OtherThumbnail, model.URL, fileName);
                    //    if (UploadPicFile(oFile, fileFullName))
                    //    {
                    //        info.Msg = "图片上传成功。";
                    //        info.FileName = fileName;
                    //    }
                    //    else
                    //    {
                    //        info.UploadStatus = false;
                    //        info.Msg = "图片上传失败,请重试。";
                    //    }
                    //}
                    //else
                    //{
                    //    info.UploadStatus = false;
                    //    info.Msg = string.Format("图片大小应为{0}*{1}。", model.Width, model.Height);
                    //}

                    int width = 0, height = 0;
                    using (Image temImg = Image.FromStream(oFile.InputStream))
                    {
                        width  = temImg.Width;
                        height = temImg.Height;
                    }

                    SaveOtherThumbnail(oFile, model.OtherThumbnail, model.URL, fileName);
                    if (UploadPicFile(oFile, fileFullName))
                    {
                        info.Msg       = "图片上传成功。";
                        info.FileName  = fileName;
                        info.ImgWidth  = width;
                        info.ImgHeight = height;
                    }
                    else
                    {
                        info.UploadStatus = false;
                        info.Msg          = "图片上传失败,请重试。";
                    }
                }
                #endregion
            }
            #endregion
            if (!string.IsNullOrEmpty(info.FileName))
            {
                info.FullFileName = model.URL + info.FileName;
            }

            return(info);
        }