Пример #1
0
        /// <summary>
        /// 系统文件保存
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public UploadImgResult SaveSystemImg(SystemImgFileCommand command)
        {
            var result  = new UploadImgResult();
            var request = command.Data;

            if (request.ContentLength > 0)
            {
                // 上传用户图片
                var targetCategory = SystemUploadPath + @"/";
                targetCategory += GetSaveFile(UserFileType.Image) + @"/";
                var option = new FileAttachOption();

                if (command.SmallImgFileSetting.IsUser)
                {
                    option.OriginalImgWidth  = command.SmallImgFileSetting.Width;
                    option.OriginalImgHeight = command.SmallImgFileSetting.Height;
                    option.BuildOriginalImg  = true;
                    option.OriginalImgModel  = command.SmallImgFileSetting.ImgModel;
                }
                else
                {
                    option.BuildOriginalImg = false;
                }

                if (command.StandardImgFileSetting.IsUser)
                {
                    option.StandardImgWidth  = command.StandardImgFileSetting.Width;
                    option.StandardImgHeight = command.StandardImgFileSetting.Height;
                    option.BuildStandardImg  = true;
                    option.StandardImgModel  = command.StandardImgFileSetting.ImgModel;
                }
                else
                {
                    option.BuildStandardImg = false;
                }

                // 允许上传文件后缀
                var fullFilePath = _fileAttachmentUtility.SaveLocalhostImgAttach(request, targetCategory, option);
                if (command.SmallImgFileSetting.IsUser)
                {
                    result.SmallImg = targetCategory + "s_" + fullFilePath;
                }
                if (command.StandardImgFileSetting.IsUser)
                {
                    result.StandardImg = targetCategory + "b_" + fullFilePath;
                }

                result.Img = targetCategory + fullFilePath;
            }
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 检查文件后缀
        /// </summary>
        /// <param name="fileAttachOption">附件</param>
        /// <param name="fileSuffix">文件后缀</param>
        private static void CheckFileSuffix(FileAttachOption fileAttachOption, string fileSuffix)
        {
            var isCheck = false;

            if (fileAttachOption.ImgSuffix != null)
            {
                foreach (var item in fileAttachOption.ImgSuffix.Where(item => item.ToLower() == fileSuffix.ToLower()))
                {
                    isCheck = true;
                }
            }
            else
            {
                isCheck = true;
            }

            if (!isCheck)
            {
                throw new BusinessException("上传的附件格式不正确");
            }
        }
Пример #3
0
        /// <summary>
        /// 保存附件
        /// </summary>
        /// <param name="file">要保存的附件</param>
        /// <param name="targetCategory">附件存放的文件夹</param>
        /// <param name="fileAttachOption">附件配置</param>
        /// <returns>返回文件名</returns>
        public string SaveLocalhostImgAttach(HttpPostedFile file, string targetCategory, FileAttachOption fileAttachOption)
        {
            var fileSuffix = Utils.GetUrlSuffix(file.FileName);

            if (fileAttachOption.ImgSuffix == null || fileAttachOption.ImgSuffix.Count() <= 0)
            {
                // 加载配置数据
                if (!string.IsNullOrEmpty(ImgSuffixConfig))
                {
                    fileAttachOption.ImgSuffix = ImgSuffixConfig.Split('|');
                }
            }

            CheckFileSuffix(fileAttachOption, fileSuffix);
            //var fileInfo = new FileInfo(file.FileName);
            // 随机生成图片名并为原图
            string saveFileName = Guid.NewGuid() + "." + fileSuffix;
            string sitePath     = targetCategory;
            string basePath     = HttpContext.Current.Server.MapPath("/") + sitePath;
            // 文件名称
            string fileNameS   = "s_" + saveFileName;                         // 缩略图文件名称
            string fileNameB   = "b_" + saveFileName;                         // 标准图文件名称
            string webFilePath = basePath + saveFileName;                     // 文件原图

            // 保存原始文件
            if (!System.IO.File.Exists(webFilePath))
            {
                // 检查是否有该文件夹,没有则创建
                if (!Directory.Exists(basePath))
                {
                    Directory.CreateDirectory(basePath);
                }

                file.SaveAs(webFilePath);
                // 生成缩略图方法
                if (fileAttachOption.BuildOriginalImg)
                {
                    ImgHelper.MakeThumbnail(webFilePath,
                                            basePath + fileNameS,
                                            fileAttachOption.OriginalImgWidth,
                                            fileAttachOption.OriginalImgHeight,
                                            fileAttachOption.OriginalImgModel.ToString());
                }
                // 生成标准图方法
                if (fileAttachOption.BuildStandardImg)
                {
                    ImgHelper.MakeThumbnail(webFilePath,
                                            basePath + fileNameB,
                                            fileAttachOption.StandardImgWidth,
                                            fileAttachOption.StandardImgHeight,
                                            fileAttachOption.StandardImgModel.ToString());
                }
                //return  sitePath + saveFileName;
                return(saveFileName);
            }
            return("");
        }