private string UploadFiled(FileUpload FileUpload1, string imgurl, string newName) { if (FileUpload1.HasFile) { string strExt = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToString(); string type = System.Configuration.ConfigurationManager.AppSettings["DownType"]; int index = type.ToLower().IndexOf(strExt.ToLower()); if (index == -1) { FileUpload1.Focus(); //return -1; return("附件类型只能为:" + type); } int size = Check.GetInt32(System.Configuration.ConfigurationManager.AppSettings["DownSize"]); int size2 = FileUpload1.PostedFile.ContentLength; if (size != 0 && size2 <= size * 1024) { FileUpload1.Focus(); //return -1; return("附件过大,最大只能为:" + size2 / 1024 / 1024 + "Mb"); } try { //上传文件并指定上传目录的路径 FileUpload1.PostedFile.SaveAs(Server.MapPath(@imgurl) + newName); /*注意->这里为什么不是:FileUpLoad1.PostedFile.FileName * 而是:FileUpLoad1.FileName? * 前者是获得客户端完整限定(客户端完整路径)名称 * 后者FileUpLoad1.FileName只获得文件名. */ //当然上传语句也可以这样写(貌似废话): //FileUpLoad1.SaveAs(@"D:\"+FileUpLoad1.FileName); //lblMessage.Text = "上传成功!"; return("上传成功"); } catch (Exception ex) { //lblMessage.Text = "出现异常,无法上传!"; //lblMessage.Text += ex.Message; return("上传附件失败,请重试"); } } return("附件不能为空"); }
protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow gvr = (GridViewRow)((DropDownList)sender).Parent.Parent; DropDownList ddlTest = gvr.FindControl("ddlTest") as DropDownList; Label lblTest = gvr.FindControl("lblTest") as Label; FileUpload fuTestReport = gvr.FindControl("fuTestReport") as FileUpload; lblTest.Visible = false; fuTestReport.Focus(); }
protected string SaveFile(FileUpload file) { string id = ""; if (file.HasFile) { Stream fs = file.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); Byte[] bytes = br.ReadBytes((Int32)fs.Length); string fileext = Path.GetExtension(file.PostedFile.FileName); string mime = MimeType.GetMimeType(bytes, file.PostedFile.FileName); if (fileext == ".jpeg" || fileext == ".JPG" || fileext == ".JPEG" || fileext == ".jpg" || fileext == ".PNG" || fileext == ".png" || fileext == ".pdf") { int len = file.PostedFile.ContentLength; if ((len / 1024) > 5120) { cf.ShowAlertMessage("File size is exceeded"); file.Focus(); } else { string filname = Path.GetFileName(file.PostedFile.FileName); // dt = Regobjdl.SaveFile(fileext, filname, len.ToString(), bytes, type, con); //objR.fileType = fileext.ToString(); //objR.filenm = filname.ToString(); //objR.size = len.ToString(); //objR.file = bytes; //objR.Action = "I"; dt = Regobjdl.SeedRegistration(objR, con); if (dt.Rows.Count > 0) { id = dt.Rows[0][0].ToString(); } } } else { cf.ShowAlertMessage("Invalid file"); } } else { cf.ShowAlertMessage("upload File"); } return(id); }
protected void lnkbtnUpload_OnClick(object sender, EventArgs e) { string msg = string.Empty; if (!FileUpload.HasFile) { FileUpload.Focus(); msg = "Please Select Excel file!"; ShowMessageErr(msg); return; } if (ddlDateRange.SelectedValue == "0") { ddlDateRange.Focus(); msg = "Please Select Date Range!"; ShowMessageErr(msg); return; } if (ddlLocation.SelectedValue == "0") { ddlLocation.Focus(); msg = "Please Select Location!"; ShowMessageErr(msg); return; } OpenAccDAL objOpenStockAccess = new OpenAccDAL(); string excelfilename = string.Empty; try { using (TransactionScope Tran = new TransactionScope(TransactionScopeOption.Required)) { excelfilename = ApplicationFunction.UploadFileServerControl(FileUpload, "ExcelAccessories", "OpeningStockAccessories"); if ((System.IO.Path.GetExtension(excelfilename) == ".xls") || (System.IO.Path.GetExtension(excelfilename) == ".xlsx")) { DataTable dt = ReadExcelFile("~/ExcelAccessories/" + excelfilename); Int64 value = 0; OpenAccDAL clsOpngStck = new OpenAccDAL(); DataTable DtnotUpload = dt.Clone(); OpenAccDAL objItem = new OpenAccDAL(); for (int count = 0; count < dt.Rows.Count; count++) { var lst = objItem.GetItemDetailsExl(Convert.ToString(dt.Rows[i]["Item"]).Trim()); if (lst.Count <= 0) { ShowMessageErr(Convert.ToString(dt.Rows[i]["Item"].ToString()).Trim() + " Item Does Not Exist!"); return; } } int dttruncate = objOpenStockAccess.TurncatePartsAccessoriesFromExcel(ApplicationFunction.ConnectionString()); value = clsOpngStck.InsertPartsByExcel(dt, ApplicationFunction.ConnectionString()); BindGridDB(); } else { msg = "Excel File Format Not Supported!"; ShowMessageErr(msg); return; } } } catch (Exception Ex) { } }
/// <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); }
public bool UploadPhoto(FileUpload fuPhoto, string pathShort, string photoName, int maxSizeKB, int intWidth, int intHeight, string strWatermark, int fontSize, out string errorMessage, out string fileName) { #region Remark /*############################ Example ############################ * Upload ไฟล์ พร้อมลดขนาดรูป * * string outError; * string outFilename; * if (!clsIO.UploadPhoto(fuPhoto, "/Upload/PhotoBook/", "Book_" + "id", 512, 150, 0, "", 0, out outError, out outFilename)) * { * lblSQL.Text = outError; * } * else * { * //ทำอะไรต่อ ถ้าอัพผ่าน * } #################################################################*/ #endregion bool rtnValue = true; errorMessage = ""; fileName = ""; if (fuPhoto.HasFile == true) { 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); } } fileName = photoName + System.IO.Path.GetExtension(fuPhoto.FileName).ToLower(); FileExist(pathShort + fileName, true); try { fuPhoto.SaveAs(System.Web.HttpContext.Current.Server.MapPath(pathShort + fileName)); } catch (Exception ex) { errorMessage = "เกิดข้อผิดพลาด ขณะอัพโหลดไฟล์ไว้ที่ " + System.Web.HttpContext.Current.Server.MapPath(pathShort + fileName); fuPhoto.Focus(); return(false); } if (!ImageResize(intWidth, intHeight, pathShort + fileName, "", strWatermark, fontSize)) { errorMessage = "เกิดข้อผิดพลาด ขณะย่อขนาดภาพ"; fuPhoto.Focus(); return(false); } } else { errorMessage = "ไม่พบไฟล์ที่ต้องการอัพโหลด"; fuPhoto.Focus(); return(false); } return(rtnValue); }
public int addRoom(string roomNo, string roomPrice, string roomType, string roomAvailable, FileUpload fileUpload, FileUpload fileUpload1) { string imgName, imgName1, imgPath, imgPath1; imgName = fileUpload.FileName.ToString(); imgName1 = fileUpload1.FileName.ToString(); string relativePathToUpload = "../../asset/images/rooms/"; string relativePath = "/asset/images/rooms/"; string absolute = Server.MapPath(relativePath); string exactPath = Path.GetFullPath(relativePath); imgPath = relativePath + imgName; imgPath1 = relativePath + imgName1; string imgPathForUpload = relativePathToUpload + imgName; string imgPathForUpload1 = relativePathToUpload + imgName1; if (fileUpload.PostedFile.ContentLength > 5048576) // 5120 KB means 5MB { ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "popUp('File is too big',false)", true); fileUpload.Focus(); } else { if (!File.Exists(Server.MapPath(imgPathForUpload))) { fileUpload.SaveAs(Server.MapPath(relativePathToUpload + imgName)); } } if (fileUpload1.PostedFile.ContentLength > 5048576) // 5120 KB means 5MB { ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "popUp('File is too big',false)", true); fileUpload1.Focus(); } else { if (!File.Exists(Server.MapPath(imgPathForUpload1))) { fileUpload1.SaveAs(Server.MapPath(relativePathToUpload + imgName1)); } } int effectedRows = 0; int roomID = 0; SqlConnection conn = new SqlConnection(DbString.connectionString); SqlCommand cmd; cmd = new SqlCommand("RoomSearch", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@roomNo", roomNo)); try { conn.Open(); int numberOfRows = Convert.ToInt32(cmd.ExecuteScalar()); if (numberOfRows >= 1) { message = "Room Number already exists."; effectedRows = 0; } else { cmd = new SqlCommand("RoomAdd", conn); //Instruct the command object to execute it cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@roomNo", roomNo)); cmd.Parameters.Add(new SqlParameter("@roomPrice", roomPrice)); cmd.Parameters.Add(new SqlParameter("@roomType", roomType)); cmd.Parameters.Add(new SqlParameter("@roomAvailable", roomAvailable)); cmd.Parameters.Add(new SqlParameter("@Thumbnail", imgPath)); roomID = Convert.ToInt32(cmd.ExecuteScalar()); cmd = new SqlCommand("RoomImagesAdd", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@roomID", roomID)); cmd.Parameters.Add(new SqlParameter("@imgName", imgName)); cmd.Parameters.Add(new SqlParameter("@imgPath", imgPath)); cmd.Parameters.Add(new SqlParameter("@imgName1", imgName1)); cmd.Parameters.Add(new SqlParameter("@imgPath1", imgPath1)); effectedRows = Convert.ToInt32(cmd.ExecuteNonQuery()); } conn.Close(); } catch (Exception ex) { message = ex.Message; Console.WriteLine(ex.Message); } return(effectedRows); }
/// <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; }
public bool UploadPhoto(FileUpload fuPhoto, string pathShort, string photoName, int maxSizeKB, int intWidth, int intHeight, string strWatermark, int fontSize, out string errorMessage, out string fileName) { #region Remark /*############################ Example ############################ Upload ไฟล์ พร้อมลดขนาดรูป string outError; string outFilename; if (!clsIO.UploadPhoto(fuPhoto, "/Upload/PhotoBook/", "Book_" + "id", 512, 150, 0, "", 0, out outError, out outFilename)) { lblSQL.Text = outError; } else { //ทำอะไรต่อ ถ้าอัพผ่าน } #################################################################*/ #endregion bool rtnValue = true; errorMessage = ""; fileName = ""; if (fuPhoto.HasFile == true) { 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; } } fileName = photoName + System.IO.Path.GetExtension(fuPhoto.FileName).ToLower(); FileExist(pathShort + fileName, true); try { fuPhoto.SaveAs(System.Web.HttpContext.Current.Server.MapPath(pathShort + fileName)); } catch (Exception ex) { errorMessage = "เกิดข้อผิดพลาด ขณะอัพโหลดไฟล์ไว้ที่ " + System.Web.HttpContext.Current.Server.MapPath(pathShort + fileName); fuPhoto.Focus(); return false; } if (!ImageResize(intWidth, intHeight, pathShort + fileName, "", strWatermark, fontSize)) { errorMessage = "เกิดข้อผิดพลาด ขณะย่อขนาดภาพ"; fuPhoto.Focus(); return false; } } else { errorMessage = "ไม่พบไฟล์ที่ต้องการอัพโหลด"; fuPhoto.Focus(); return false; } return rtnValue; }