Example #1
0
        /// <summary>
        /// 保存远程文件到本地
        /// </summary>
        /// <param name="fileUri">URI地址</param>
        /// <returns>上传后的路径</returns>
        public string RemoteSaveAs(string fileUri)
        {
            WebClient client         = new WebClient();
            string    fileExt        = fileUri.LastIndexOf(".", StringComparison.Ordinal) == -1 ? "gif" : Utils.GetFileExt(fileUri);
            string    newFileName    = Utils.GetGuidString() + "." + fileExt; //随机生成新的文件名
            string    upLoadPath     = GetUpLoadPath();                       //上传目录相对路径
            string    fullUpLoadPath = Utils.GetMapPath(upLoadPath);          //上传目录的物理路径
            string    newFilePath    = upLoadPath + newFileName;              //上传后的路径

            //检查上传的物理路径是否存在,不存在则创建
            if (!Directory.Exists(fullUpLoadPath))
            {
                Directory.CreateDirectory(fullUpLoadPath);
            }

            try
            {
                client.DownloadFile(fileUri, fullUpLoadPath + newFileName);
                //如果是图片,检查是否需要打水印
                if (IsWaterMark(fileExt))
                {
                    switch (watermarktype)
                    {
                    case 1:
                        WaterMark.AddImageSignText(newFilePath, newFilePath,
                                                   _config["watermarktext"], watermarkposition,
                                                   watermarkimgquality, _config["watermarkfont"], watermarkfontsize);
                        break;

                    case 2:
                        WaterMark.AddImageSignPic(newFilePath, newFilePath, _config["watermarkpic"], watermarkposition,
                                                  watermarkimgquality, watermarktransparency);
                        break;
                    }
                }
            }
            catch
            {
                return(string.Empty);
            }
            client.Dispose();
            return(newFilePath);
        }
Example #2
0
        /// <summary>
        /// 文件上传方法
        /// </summary>
        /// <param name="postedFile">文件流</param>
        /// <param name="isThumbnail">是否生成缩略图</param>
        /// <param name="isWater">是否打水印</param>
        /// <returns>上传后文件信息</returns>
        public string FileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater)
        {
            try
            {
                string fileExt              = Utils.GetFileExt(postedFile.FileName);                                                              //文件扩展名,不含“.”
                int    fileSize             = postedFile.ContentLength;                                                                           //获得文件大小,以字节为单位
                string fileName             = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\", StringComparison.Ordinal) + 1); //取得原文件名
                string newFileName          = Utils.GetGuidString() + "." + fileExt;                                                              //随机生成新的文件名
                string newThumbnailFileName = "thumb_" + newFileName;                                                                             //随机生成缩略图文件名
                string upLoadPath           = GetUpLoadPath();                                                                                    //上传目录相对路径
                string fullUpLoadPath       = Utils.GetMapPath(upLoadPath);                                                                       //上传目录的物理路径
                string newFilePath          = upLoadPath + newFileName;                                                                           //上传后的路径
                string newThumbnailPath     = upLoadPath + newThumbnailFileName;                                                                  //上传后的缩略图路径
                //检查文件扩展名是否合法
                if (!CheckFileExt(fileExt))
                {
                    return("{\"status\": 0, \"msg\": \"不允许上传" + fileExt + "类型的文件!\"}");
                }
                //检查文件大小是否合法
                if (!CheckFileSize(fileExt, fileSize))
                {
                    return("{\"status\": 0, \"msg\": \"文件超过限制的大小!\"}");
                }
                //检查上传的物理路径是否存在,不存在则创建
                if (!Directory.Exists(fullUpLoadPath))
                {
                    Directory.CreateDirectory(fullUpLoadPath);
                }

                //保存文件
                postedFile.SaveAs(fullUpLoadPath + newFileName);
                //如果是图片,检查图片是否超出最大尺寸,是则裁剪
                if (IsImage(fileExt))
                {
                    Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName,
                                                 imgmaxwidth, imgmaxheight);
                }
                //如果是图片,检查是否需要生成缩略图,是则生成
                if (IsImage(fileExt) && isThumbnail && thumbnailwidth > 0 && thumbnailheight > 0)
                {
                    Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName,
                                                 thumbnailwidth, thumbnailheight, "Cut");
                }
                else
                {
                    newThumbnailPath = newFilePath; //不生成缩略图则返回原图
                }
                //如果是图片,检查是否需要打水印
                if (IsWaterMark(fileExt) && isWater)
                {
                    switch (watermarktype)
                    {
                    case 1:
                        WaterMark.AddImageSignText(newFilePath, newFilePath,
                                                   _config["watermarktext"], watermarkposition,
                                                   watermarkimgquality, _config["watermarkfont"], watermarkfontsize);
                        break;

                    case 2:
                        WaterMark.AddImageSignPic(newFilePath, newFilePath,
                                                  _config["watermarkpic"], watermarkposition,
                                                  watermarkimgquality, watermarktransparency);
                        break;
                    }
                }
                //处理完毕,返回JOSN格式的文件信息
                return("{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \""
                       + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \""
                       + newThumbnailPath + "\", \"size\": " + fileSize + ", \"ext\": \"" + fileExt + "\"}");
            }
            catch
            {
                return("{\"status\": 0, \"msg\": \"上传过程中发生意外错误!\"}");
            }
        }