Пример #1
0
        //public string FileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater, bool isImage)
        //{
        //    return FileSaveAs(postedFile, isThumbnail, isWater, isImage, false);
        //}

        /// <summary>
        /// 文件上传方法
        /// </summary>
        /// <param name="postedFile">文件流</param>
        /// <param name="isThumbnail">是否生成缩略图</param>
        /// <param name="isWater">是否添加水印</param>
        /// <param name="isImage"></param>
        /// <param name="isReOriginal">是否使用原始名字存储文件</param>
        /// <returns>上传后的文件信息</returns>
        public string FileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater, bool isImage = false, bool isReOriginal = false)
        {
            var fileType = Utilities.GetFileTypeName(postedFile.FileName);
            var fileSize = postedFile.ContentLength;
            var fileName =
                postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
            var newFileName = Utilities.GetRamCodeOnDate() + "." + fileType;

            if (isReOriginal)
            {
                //如果 isReOriginal为True,那就使用原始名字
                newFileName = fileName;
            }
            if (!CheckFileExt(fileType))
            {
                return("{\"msg\": \"0\", \"msgbox\": \"不允许上传" + fileType + "类型的文件!\"}");
            }
            if (isImage && !IsImage(fileType))
            {
                return("{\"msg\": \"0\", \"msgbox\": \"对不起,仅允许上传图片文件!\"}");
            }
            if (!CheckFileSize(fileSize))
            {
                return("{\"msg\": \"0\", \"msgbox\": \"上传内容大小超出限制\"}");
            }

            var dirPath = GetUpLoadPath();

            //存储至物理路径
            var toFileFullPath = Utilities.GetMapPath(dirPath);

            if (!Directory.Exists(toFileFullPath))
            {
                Directory.CreateDirectory(toFileFullPath);
            }
            postedFile.SaveAs(toFileFullPath + newFileName);

            var serverFileName          = dirPath + newFileName;
            var serverThumbnailFileName = dirPath + "thumb_" + newFileName;

            var returnFileName = serverFileName;

            //如果是图片,检查图片尺寸是否超出限制
            if (IsImage(fileType) && (_siteConfig.AttachImgMaxHeight > 0 || _siteConfig.AttachImgMaxWidth > 0))
            {
                Thumbnail.MakeThumbnailImage(toFileFullPath + newFileName, toFileFullPath + newFileName, _siteConfig.AttachImgMaxWidth, _siteConfig.AttachImgMaxHeight);
            }
            //是否生成缩略图
            if (IsImage(fileType) && isThumbnail && _siteConfig.ThumbnailWidth > 0 && _siteConfig.ThumbnailHeight > 0)
            {
                //参数“cut"表示切掉而不按照比例裁剪
                Thumbnail.MakeThumbnailImage(toFileFullPath + newFileName, toFileFullPath + "thumb_" + newFileName, _siteConfig.ThumbnailWidth, _siteConfig.ThumbnailHeight, "Cut");
                //增加缩略图相对路径,以逗号分隔开
                returnFileName += "," + serverThumbnailFileName;
            }
            //是否打图片水印
            if (IsWaterMark(fileType) && isWater)
            {
                switch (_siteConfig.WatermarkType)
                {
                case 1:
                    WaterMark.AddImageSignText(serverFileName, serverFileName,
                                               _siteConfig.WatermarkText, _siteConfig.WatermarkPosition,
                                               _siteConfig.WatermarkImgQuality, _siteConfig.WatermarkFont, _siteConfig.WatermarkFontSize);
                    break;

                case 2:
                    WaterMark.AddImageSignPic(serverFileName, serverFileName,
                                              _siteConfig.WatermarkPic, _siteConfig.WatermarkPosition,
                                              _siteConfig.WatermarkImgQuality, _siteConfig.WatermarkTransparency);
                    break;
                }
            }
            //如果需要返回原文件名
            if (isReOriginal)
            {
                return("{\"msg\": \"1\", \"msgbox\": \"" + returnFileName + "\", mstitle: \"" + fileName + "\"}");
            }
            return("{\"msg\": \"1\", \"msgbox\": \"" + returnFileName + "\"}");
        }
Пример #2
0
        /// <summary>
        /// 用于图片分享保存文件内容
        /// </summary>
        /// <param name="postedFile"></param>
        /// <param name="isThumbnail"></param>
        /// <param name="isWater"></param>
        /// <param name="isReOriginal"></param>
        /// <returns>返回PhotoType文件类型</returns>
        public PhotoDto PictureSaveAs(HttpPostedFileBase postedFile, bool isThumbnail, bool isWater, bool isReOriginal)
        {
            var newPhoto = new PhotoDto();
            var fileType = Utilities.GetFileTypeName(postedFile.FileName);

            if (!CheckFileExt(fileType) || !IsImage(fileType))
            {
                newPhoto.PhotoId = Guid.Empty;
                newPhoto.Remark  = @"文件类型为非图片类型";
                return(newPhoto);
            }
            var fileSize    = postedFile.ContentLength;
            var fileName    = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
            var temp        = Utilities.GetRamCodeOnDate();
            var newFileName = temp + "." + fileType;

            if (isReOriginal)
            {
                newFileName = fileName;
            }
            var dirPath = GetUpLoadPath();

            if (!CheckFileSize(fileSize))
            {
                newPhoto.PhotoId = Guid.Empty;
                newPhoto.Remark  = @"文件大小超出最高限制";
            }

            var serverFileName          = dirPath + newFileName;
            var serverThumbnailFileName = dirPath + "thumb_" + newFileName;
            //var returnFileName = serverFileName;
            var toFileFullPath = Utilities.GetMapPath(dirPath);

            if (!Directory.Exists(toFileFullPath))
            {
                Directory.CreateDirectory(toFileFullPath);
            }
            postedFile.SaveAs(toFileFullPath + newFileName);


            //检查图片尺寸是否超出限制
            if (_siteConfig.AttachImgMaxHeight > 0 || _siteConfig.AttachImgMaxWidth > 0)
            {
                Thumbnail.MakeThumbnailImage(toFileFullPath + newFileName, toFileFullPath + newFileName, _siteConfig.AttachImgMaxWidth, _siteConfig.AttachImgMaxHeight);
            }

            newPhoto.PhotoId   = Guid.NewGuid();
            newPhoto.PhotoPath = serverFileName;

            //是否生成缩略图
            if (isThumbnail && _siteConfig.ThumbnailWidth > 0 && _siteConfig.ThumbnailHeight > 0)
            {
                Thumbnail.MakeThumbnailImage(toFileFullPath + newFileName, toFileFullPath + "thumb_" + newFileName, _siteConfig.ThumbnailWidth, _siteConfig.ThumbnailHeight, "Cut");
                //returnFileName += "," + serverThumbnailFileName; //返回缩略图,以逗号分隔开
                newPhoto.ThumbPath = serverThumbnailFileName;
            }
            //是否打图片水印
            if (IsWaterMark(fileType) && isWater)
            {
                switch (_siteConfig.WatermarkType)
                {
                case 1:
                    WaterMark.AddImageSignText(serverFileName, serverFileName,
                                               _siteConfig.WatermarkText, _siteConfig.WatermarkPosition,
                                               _siteConfig.WatermarkImgQuality, _siteConfig.WatermarkFont, _siteConfig.WatermarkFontSize);
                    break;

                case 2:
                    WaterMark.AddImageSignPic(serverFileName, serverFileName,
                                              _siteConfig.WatermarkPic, _siteConfig.WatermarkPosition,
                                              _siteConfig.WatermarkImgQuality, _siteConfig.WatermarkTransparency);
                    break;
                }
            }

            return(newPhoto);
        }