コード例 #1
0
 private void ProcessPostedFile(string saveFullName, HttpPostedFileBase postedFile, string targetFilePath, string uploadFileType)
 {
     try
     {
         _Logger.Debug("begin to update file to :{0}", targetFilePath);
         string saveDir = Path.Combine(Server.MapPath("~/"), targetFilePath);
         if (Directory.Exists(saveDir) == false)//如果不存在就创建file文件夹
         {
             Directory.CreateDirectory(saveDir);
         }
         if (postedFile != null)
         {
             string strPath = Path.Combine(saveDir, saveFullName);
             if ("image".Equals(uploadFileType, StringComparison.OrdinalIgnoreCase))
             {
                 //1.压缩原图, 最大尺寸, 宽1024, 不限制高
                 ImageUtility.MakeThumbnail(null, postedFile.InputStream, strPath, 1024, 768, "W");
                 //2.生成200*200的缩略图, _T 结尾
                 ImageUtility.MakeThumbnail(null, postedFile.InputStream, strPath.Insert(strPath.LastIndexOf('.'), "_T"), 200, 200, "Cut");
                 //3.图文封面生成宽900*500的等比图, 由于900*500的容易超过64kg, 等比压缩为360*220
                 bool isNewsCover = false;
                 if (bool.TryParse(Request["isNewsCover"], out isNewsCover) && isNewsCover)
                 {
                     int appId  = -1;
                     int width  = 360;
                     int height = 220;
                     try
                     {
                         if (int.TryParse(Request["AppId"], out appId) && appId > -1)
                         {
                             var accoutManagement = WeChatCommonService.GetAccountManageByWeChatID(appId);
                             if (null != accoutManagement && null != accoutManagement.AccountType && accoutManagement.AccountType.Value == 0)
                             {
                                 width  = 900;
                                 height = 500;
                             }
                         }
                     }
                     catch (Exception ex)
                     {
                         _Logger.Error(ex);
                     }
                     _Logger.Debug("_T width:{0} height:{1}", width, height);
                     ImageUtility.MakeThumbnail(null, postedFile.InputStream, strPath.Insert(strPath.LastIndexOf('.'), "_B"), width, height, "W");
                 }
             }
             else
             {
                 postedFile.SaveAs(strPath);
             }
         }
         _Logger.Debug("success to upload file.");
     }
     catch (Exception ex)
     {
         _Logger.Error(ex, "Upload file failed");
         throw ex;
     }
 }