Ejemplo n.º 1
0
        /// <summary>
        /// 裁剪图片并保存
        /// </summary>
        public bool cropSaveAs(string fileName, string newFileName, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y, out string fileHash)
        {
            fileHash = "";
            string fileExt = Utils.GetFileExt(fileName); //文件扩展名,不含“.”

            if (!IsImage(fileExt))
            {
                return(false);
            }
            string newFileDir = Utils.GetMapPath(newFileName.Substring(0, newFileName.LastIndexOf(@"/") + 1));

            //检查是否有该路径,没有则创建
            if (!Directory.Exists(newFileDir))
            {
                Directory.CreateDirectory(newFileDir);
            }
            try
            {
                string fileFullPath   = Utils.GetMapPath(fileName);
                string toFileFullPath = Utils.GetMapPath(newFileName);
                bool   result         = Thumbnail.MakeThumbnailImage(fileFullPath, toFileFullPath, 180, 180, cropWidth, cropHeight, X, Y);
                if (result)
                {
                    //存储裁剪图到数据库
                    Model.files modelNewFile = new BLL.files().Add(newFileName, userID, true);
                    fileHash = Utils.GetAppSettings("FileUrl") + modelNewFile.file_md5;
                }
                return(result);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 保存远程文件到本地
        /// </summary>
        /// <param name="fileUri">URI地址</param>
        /// <returns>上传后的路径</returns>
        public string remoteSaveAs(string fileUri)
        {
            WebClient client  = new WebClient();
            string    fileExt = string.Empty; //文件扩展名,不含“.”

            if (fileUri.LastIndexOf(".") == -1)
            {
                fileExt = "gif";
            }
            else
            {
                fileExt = Utils.GetFileExt(fileUri);
            }
            string newFileName    = Utils.GetRamCode() + "." + fileExt; //随机生成新的文件名
            string upLoadPath     = GetUpLoadPath();                    //上传目录相对路径
            string fullUpLoadPath = Utils.GetMapPath(upLoadPath);       //上传目录的物理路径
            string newFilePath    = upLoadPath + newFileName;           //上传后的路径

            Model.files modelNewFile = new Model.files();               //新文件所需实体类
            //检查上传的物理路径是否存在,不存在则创建
            if (!Directory.Exists(fullUpLoadPath))
            {
                Directory.CreateDirectory(fullUpLoadPath);
            }

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

                    case 2:
                        WaterMark.AddImageSignPic(newFilePath, newFilePath,
                                                  this.siteConfig.watermarkpic, this.siteConfig.watermarkposition,
                                                  this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency);
                        break;
                    }
                }
                //存储文件信息
                modelNewFile = new BLL.files().Add(newFilePath, userID);
            }
            catch
            {
                return(string.Empty);
            }
            client.Dispose();
            return(Utils.GetAppSettings("FileUrl") + modelNewFile.file_md5);
        }
Ejemplo n.º 3
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(@"\") + 1); //取得原文件名
                string      newFileName          = Utils.GetRamCode() + "." + fileExt;                                       //随机生成新的文件名
                string      newThumbnailFileName = "thumb_" + newFileName;                                                   //随机生成缩略图文件名
                string      upLoadPath           = GetUpLoadPath();                                                          //上传目录相对路径
                string      fullUpLoadPath       = Utils.GetMapPath(upLoadPath);                                             //上传目录的物理路径
                string      newFilePath          = upLoadPath + newFileName;                                                 //上传后的路径
                string      newThumbnailPath     = upLoadPath + newThumbnailFileName;                                        //上传后的缩略图路径
                Model.files modelThumbnail       = new Model.files();                                                        //缩略图所需实体类
                Model.files modelNewFile         = new Model.files();                                                        //新上传文件所需实体类

                //检查文件扩展名是否合法
                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) && (this.siteConfig.imgmaxheight > 0 || this.siteConfig.imgmaxwidth > 0))
                {
                    Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName,
                                                 this.siteConfig.imgmaxwidth, this.siteConfig.imgmaxheight);
                }

                //如果是图片,检查是否需要生成缩略图,是则生成
                if (IsImage(fileExt) && isThumbnail && this.siteConfig.thumbnailwidth > 0 && this.siteConfig.thumbnailheight > 0)
                {
                    Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName,
                                                 this.siteConfig.thumbnailwidth, this.siteConfig.thumbnailheight, this.siteConfig.thumbnailmode);
                    //存储文件信息
                    modelThumbnail = new BLL.files().Add(newThumbnailPath, userID);
                }
                else
                {
                    newThumbnailPath = newFilePath; //不生成缩略图则返回原图
                }

                //如果是图片,检查是否需要打水印
                if (IsWaterMark(fileExt) && isWater)
                {
                    switch (this.siteConfig.watermarktype)
                    {
                    case 1:
                        WaterMark.AddImageSignText(newFilePath, newFilePath,
                                                   this.siteConfig.watermarktext, this.siteConfig.watermarkposition,
                                                   this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize);
                        break;

                    case 2:
                        WaterMark.AddImageSignPic(newFilePath, newFilePath,
                                                  this.siteConfig.watermarkpic, this.siteConfig.watermarkposition,
                                                  this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency);
                        break;
                    }
                }

                //存储文件信息
                modelNewFile = new BLL.files().Add(newFilePath, userID);
                if (string.IsNullOrEmpty(modelThumbnail.file_path))
                {
                    modelThumbnail = modelNewFile;
                }

                //处理完毕,返回JOSN格式的文件信息
                return("{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \""
                       + modelNewFile.file_name + "\", \"path\": \"" + Utils.GetAppSettings("FileUrl") + modelNewFile.file_md5 + "\", \"thumb\": \""
                       + Utils.GetAppSettings("FileUrl") + modelThumbnail.file_md5 + "\", \"size\": " + fileSize + ", \"ext\": \"" + fileExt + "\"}");
            }
            catch
            {
                return("{\"status\": 0, \"msg\": \"上传过程中发生意外错误!\"}");
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 根据MD5值下载文件并显示
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void Page_Load(object sender, EventArgs e)
 {
     try
     {
         //获得来源参数
         string FileHashValue = HttpContext.Current.Request.Url.Query.ToString();
         if (FileHashValue.StartsWith("?"))
         {
             FileHashValue = FileHashValue.Substring(1);
         }
         //判断来源HASH是否为空
         if (string.IsNullOrEmpty(FileHashValue))
         {
             Response.End();
         }
         //判断来源Hash是否存在于缓存
         Model.files model = CacheHelperRedis.Get <Model.files>(fileCacheName, FileHashValue);
         BLL.files   bll   = new BLL.files();
         if (model == null)
         {
             model = bll.GetModel(FileHashValue);
             if (model != null)
             {
                 //写入缓存
                 CacheHelperRedis.Insert(fileCacheName, FileHashValue, model);
             }
         }
         //根据文件信息下载文件
         if (model != null)
         {
             //增加文件访问次数
             bll.UpdateField(model.id, "file_usetimes=file_usetimes+1");
             //检测是否被禁用或者删除
             if (model.file_state != -1)
             {
                 //从原始服务器上下载文件
                 remoteSaveAs(model);
                 if (model.file_type == 0)
                 {
                     //如果是图片类型则进行缓存控制并输出
                     string suffix = Utils.GetFileExt(model.file_path);
                     Response.ContentType = string.Format("image/{0}", suffix.ToLower().Equals("png") ? "x-png" : suffix);
                     DateTime contentModified = System.IO.File.GetLastWriteTime(Utils.GetMapPath(model.file_path));
                     if (IsClientCached(contentModified))
                     {
                         Response.StatusCode      = 304;
                         Response.SuppressContent = true;
                     }
                     else
                     {
                         Response.Cache.SetETagFromFileDependencies();
                         Response.Cache.SetAllowResponseInBrowserHistory(true);
                         Response.Cache.SetLastModified(contentModified);
                         FileStream fs     = new FileStream(Utils.GetMapPath(model.file_path), FileMode.Open, FileAccess.Read);
                         byte[]     byData = new byte[fs.Length];
                         fs.Read(byData, 0, byData.Length);
                         fs.Close();
                         System.IO.MemoryStream ms  = new System.IO.MemoryStream(byData);
                         System.Drawing.Image   img = System.Drawing.Image.FromStream(ms);
                         img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                         Response.BinaryWrite(ms.ToArray()); //新增内容
                         img.Dispose();
                     }
                 }
                 else
                 {
                     //其它类型文件则直接提供下载
                     Response.Clear();
                     Response.Buffer = true;
                     Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(model.file_name, System.Text.Encoding.UTF8));
                     Response.WriteFile(Server.MapPath(model.file_path));
                     Response.Flush();
                     Response.Close();
                 }
             }
         }
         Response.End();
     }
     catch
     {
         Response.End();
     }
 }