/// <summary> /// อัพโหลดภาพ พร้อมย่อขนาด /// </summary> /// <param name="fuPhoto"></param> /// <param name="pathShort"></param> /// <param name="photoName"></param> /// <param name="errorMessage"></param> /// <param name="outFileName"></param> /// <param name="maxSizeKB"></param> /// <param name="maxWidth"></param> /// <param name="maxHeight"></param> /// <param name="resizeAnchor"></param> /// <param name="resizeMode"></param> /// <param name="resizeFormat"></param> /// <returns></returns> /// <example> /// clsIO clsIO = new clsIO(); /// string outErrorMessage; /// string outFileName; /// clsIO.UploadPhoto(fuPhoto, "/Upload/", "TestResize", out outErrorMessage, out outFileName); /// clsIO.UploadPhoto(fuPhoto, "/Upload/", "TestResize", out outErrorMessage, out outFileName,maxWidth:200,resizeFormat:clsIO.ResizeFormat.png); /// clsIO.UploadPhoto(fuPhoto, "/Upload/", "TestResize", out outErrorMessage, out outFileName,maxWidth:200,maxHeight:200,resizeFormat:clsIO.ResizeFormat.png); /// clsIO.UploadPhoto(fuPhoto, "/Upload/", "TestResize", out outErrorMessage, out outFileName,maxWidth:200,maxHeight:200,resizeMode:clsIO.ResizeMode.crop,resizeQuality:100); /// </example> public bool UploadPhoto(FileUpload fuPhoto, string pathShort, string photoName, out string errorMessage, out string outFileName, int maxSizeKB = 0, int maxWidth = 0, int maxHeight = 0, ResizeAnchor resizeAnchor = ResizeAnchor.middlecenter, ResizeMode resizeMode = ResizeMode.crop, ResizeFormat resizeFormat = ResizeFormat.original, int resizeQuality = 90) { bool rtnValue = true; errorMessage = ""; outFileName = ""; if (fuPhoto.HasFile == true) { #region Error Checker if (FileTypeChecker(fuPhoto.FileName) != "IMG") { errorMessage = "โปรดเลือกเฉพาะไฟล์รูปภาพ"; fuPhoto.Focus(); return(false); } if (maxSizeKB > 0) { if (fuPhoto.PostedFile.ContentLength > maxSizeKB * 1000) { errorMessage = "ขนาดไฟล์ใหญ่เกิน " + maxSizeKB + " KB"; fuPhoto.Focus(); return(false); } } #endregion #region Out FileName if (resizeFormat != ResizeFormat.original) { outFileName = photoName + "." + resizeFormat.ToString(); } else { outFileName = photoName + System.IO.Path.GetExtension(fuPhoto.FileName).ToLower(); } #endregion FileExist(pathShort + outFileName, true); #region Upload Photo try { fuPhoto.SaveAs(System.Web.HttpContext.Current.Server.MapPath(pathShort + outFileName)); } catch (Exception ex) { errorMessage = "เกิดข้อผิดพลาด ขณะอัพโหลดไฟล์ไว้ที่ " + System.Web.HttpContext.Current.Server.MapPath(pathShort + outFileName + "<br/>" + ex.Message); fuPhoto.Focus(); return(false); } #endregion #region Resize Photo try { #region Condition Builder string resizeSetting = ""; if (maxWidth > 0) { if (resizeSetting.Length > 0) { resizeSetting += "&"; } resizeSetting += "width=" + maxWidth.ToString(); } if (maxHeight > 0) { if (resizeSetting.Length > 0) { resizeSetting += "&"; } resizeSetting += "height=" + maxHeight.ToString(); } if (resizeMode != ResizeMode.pad) { if (resizeSetting.Length > 0) { resizeSetting += "&"; } resizeSetting += "mode=" + resizeMode.ToString(); } if (resizeAnchor != ResizeAnchor.middlecenter) { if (resizeSetting.Length > 0) { resizeSetting += "&"; } resizeSetting += "anchor=" + resizeAnchor.ToString(); } if (resizeFormat != ResizeFormat.original) { if (resizeSetting.Length > 0) { resizeSetting += "&"; } resizeSetting += "format=" + resizeFormat.ToString(); } if (resizeQuality != 90) { if (resizeSetting.Length > 0) { resizeSetting += "&"; } resizeSetting += "quality=" + resizeQuality.ToString(); } #endregion if (resizeSetting.Length > 0) { ResizeSettings imageResizerSetting = new ResizeSettings(resizeSetting); ImageBuilder.Current.Build( System.Web.HttpContext.Current.Server.MapPath(pathShort + outFileName), System.Web.HttpContext.Current.Server.MapPath(pathShort + outFileName), imageResizerSetting); } } catch (Exception ex) { errorMessage = "เกิดข้อผิดพลาด ขณะย่อขนาดภาพ : " + ex.Message; fuPhoto.Focus(); return(false); } #endregion } else { errorMessage = "ไม่พบไฟล์ที่ต้องการอัพโหลด"; fuPhoto.Focus(); return(false); } return(rtnValue); }
/// <summary> /// อัพโหลดภาพ พร้อมย่อขนาด /// </summary> /// <param name="fuPhoto"></param> /// <param name="pathShort"></param> /// <param name="photoName"></param> /// <param name="errorMessage"></param> /// <param name="outFileName"></param> /// <param name="maxSizeKB"></param> /// <param name="maxWidth"></param> /// <param name="maxHeight"></param> /// <param name="resizeAnchor"></param> /// <param name="resizeMode"></param> /// <param name="resizeFormat"></param> /// <returns></returns> /// <example> /// clsIO clsIO = new clsIO(); /// string outErrorMessage; /// string outFileName; /// clsIO.UploadPhoto(fuPhoto, "/Upload/", "TestResize", out outErrorMessage, out outFileName); /// clsIO.UploadPhoto(fuPhoto, "/Upload/", "TestResize", out outErrorMessage, out outFileName,maxWidth:200,resizeFormat:clsIO.ResizeFormat.png); /// clsIO.UploadPhoto(fuPhoto, "/Upload/", "TestResize", out outErrorMessage, out outFileName,maxWidth:200,maxHeight:200,resizeFormat:clsIO.ResizeFormat.png); /// clsIO.UploadPhoto(fuPhoto, "/Upload/", "TestResize", out outErrorMessage, out outFileName,maxWidth:200,maxHeight:200,resizeMode:clsIO.ResizeMode.crop,resizeQuality:100); /// </example> public bool UploadPhoto(FileUpload fuPhoto, string pathShort, string photoName, out string errorMessage, out string outFileName, int maxSizeKB = 0, int maxWidth = 0, int maxHeight = 0, ResizeAnchor resizeAnchor = ResizeAnchor.middlecenter, ResizeMode resizeMode = ResizeMode.crop, ResizeFormat resizeFormat = ResizeFormat.original, int resizeQuality = 90) { bool rtnValue = true; errorMessage = ""; outFileName = ""; if (fuPhoto.HasFile == true) { #region Error Checker if (FileTypeChecker(fuPhoto.FileName) != "IMG") { errorMessage = "โปรดเลือกเฉพาะไฟล์รูปภาพ"; fuPhoto.Focus(); return false; } if (maxSizeKB > 0) { if (fuPhoto.PostedFile.ContentLength > maxSizeKB * 1000) { errorMessage = "ขนาดไฟล์ใหญ่เกิน " + maxSizeKB + " KB"; fuPhoto.Focus(); return false; } } #endregion #region Out FileName if (resizeFormat != ResizeFormat.original) { outFileName = photoName + "." + resizeFormat.ToString(); } else { outFileName = photoName + System.IO.Path.GetExtension(fuPhoto.FileName).ToLower(); } #endregion FileExist(pathShort + outFileName, true); #region Upload Photo try { fuPhoto.SaveAs(System.Web.HttpContext.Current.Server.MapPath(pathShort + outFileName)); } catch (Exception ex) { errorMessage = "เกิดข้อผิดพลาด ขณะอัพโหลดไฟล์ไว้ที่ " + System.Web.HttpContext.Current.Server.MapPath(pathShort + outFileName + "<br/>" + ex.Message); fuPhoto.Focus(); return false; } #endregion #region Resize Photo try { #region Condition Builder string resizeSetting = ""; if (maxWidth > 0) { if (resizeSetting.Length > 0) resizeSetting += "&"; resizeSetting += "width=" + maxWidth.ToString(); } if (maxHeight > 0) { if (resizeSetting.Length > 0) resizeSetting += "&"; resizeSetting += "height=" + maxHeight.ToString(); } if (resizeMode != ResizeMode.pad) { if (resizeSetting.Length > 0) resizeSetting += "&"; resizeSetting += "mode=" + resizeMode.ToString(); } if (resizeAnchor != ResizeAnchor.middlecenter) { if (resizeSetting.Length > 0) resizeSetting += "&"; resizeSetting += "anchor=" + resizeAnchor.ToString(); } if (resizeFormat != ResizeFormat.original) { if (resizeSetting.Length > 0) resizeSetting += "&"; resizeSetting += "format=" + resizeFormat.ToString(); } if (resizeQuality != 90) { if (resizeSetting.Length > 0) resizeSetting += "&"; resizeSetting += "quality=" + resizeQuality.ToString(); } #endregion if (resizeSetting.Length > 0) { ResizeSettings imageResizerSetting = new ResizeSettings(resizeSetting); ImageBuilder.Current.Build( System.Web.HttpContext.Current.Server.MapPath(pathShort + outFileName), System.Web.HttpContext.Current.Server.MapPath(pathShort + outFileName), imageResizerSetting); } } catch (Exception ex) { errorMessage = "เกิดข้อผิดพลาด ขณะย่อขนาดภาพ : " + ex.Message; fuPhoto.Focus(); return false; } #endregion } else { errorMessage = "ไม่พบไฟล์ที่ต้องการอัพโหลด"; fuPhoto.Focus(); return false; } return rtnValue; }