protected void btnSavePhoto_Click(object sender, ImageClickEventArgs e) { if (!FileUpload1.HasFile) { return; } string fileName = FileUpload1.FileName; string strExtent = fileName.Substring(fileName.LastIndexOf(".")); fileName = CommonFunction.Get_DynamicString() + strExtent; string fileName1 = "m_" + fileName; string fileName2 = "v_" + fileName; string filePath = Server.MapPath("~/RoadPhoto/" + fileName); string filePath1 = Server.MapPath("~/RoadPhoto/" + fileName1); string filePath2 = Server.MapPath("~/RoadPhoto/" + fileName2); FileUpload1.PostedFile.SaveAs(filePath); ImgFunction.MakeThumbnail(filePath, filePath1, 600, 480, "W"); ImgFunction.MakeThumbnail(filePath, filePath2, 150, 120, "W"); System.IO.File.Delete(filePath); Image1.ImageUrl = "../RoadPhoto/" + fileName1; string sqlStr = "update R_RoadDepart set RD_MainPhoto='" + fileName1 + "',RD_ViewPhoto='" + fileName2 + "' where RD_ID='" + ViewState["rdid"].ToString() + "'"; DBHelper.ExecuteCommand(sqlStr); }
public static string GetUpLoadImgPath(FileUpload fileUpload, string pathroot, int width, int height, string model) { string webfilePath_True = ""; //上传并预览 if (fileUpload.HasFile) { string fileContentType = fileUpload.PostedFile.ContentType; if ((fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg" || fileContentType == "image/png") && fileUpload.PostedFile.ContentLength <= 3000000)//文件大于3M不能过滤 { Random ran = new Random(); string strran = ran.Next(1, 999).ToString(); //随机数转字符型 string name = fileUpload.PostedFile.FileName; //客户端文件路径 FileInfo file = new FileInfo(name); string path = ConfigurationManager.AppSettings["imagePath"].ToString(); #region 称 string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + strran + file.Extension; //文件名称,使用FileInfo,Extension得到后缀 string fileName_True = "sTrue_" + fileName; //实际存储的图片 #endregion #region 物理路径 string filePath = pathroot + fileName; string filePath_True = pathroot + fileName_True; #endregion #region 存储路径 string webFilePath = path + fileName; webfilePath_True = path + fileName_True; #endregion if (!File.Exists(filePath)) { try { fileUpload.SaveAs(filePath); MakeThumbnail(filePath, filePath_True, width, height, model); //生成缩略图 //修改url,显示图片 if (ImgFunction.FilePicDelete(filePath)) //删除源文件 { } } catch (Exception ex) { return(ex.Message); } } } } return(webfilePath_True); }
protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { string strAction = ViewState["strAction"].ToString(); string strContent = PubClass.Tool.CheckStr(Request.Form["t_contents"].ToString()); if (cboSel.Checked) { if (strAction == "add" && !FileUpload1.HasFile) { return; } if (FileUpload1.HasFile) { string fileName = FileUpload1.FileName; string strExtent = fileName.Substring(fileName.LastIndexOf(".")); fileName = CommonFunction.Get_DynamicString() + strExtent; string fileName1 = "v_" + fileName; string fileName2 = "vv_" + fileName; string filePath = Server.MapPath("~/NewsImg/" + fileName); string filePath1 = Server.MapPath("~/NewsImg/" + fileName1); string filePath2 = Server.MapPath("~/NewsImg/" + fileName2); FileUpload1.PostedFile.SaveAs(filePath); ImgFunction.MakeThumbnail(filePath, filePath1, 600, 400, "W"); ImgFunction.MakeThumbnail(filePath, filePath2, 180, 120, "W"); System.IO.File.Delete(filePath); ViewState["ImgPath"] = fileName1; ViewState["ImgView"] = fileName2; } } string sqlStr = ""; if (strAction == "add") { sqlStr = "insert into S_NewsInfo(N_Title,N_Content,N_From,N_Date,N_TSID,N_TID,N_ImgPath,N_ImgView) values('" + txtTitle.Text.Trim() + "','" + strContent + "','" + txtFrom.Text.Trim() + "','" + txtDate.Text.Trim() + "','" + ((UserInfo)Session["StationUser"]).U_TollStation.TS_ID + "','" + ((DataTable)ViewState["dtType"]).Rows[0]["T_ID"].ToString() + "','" + ViewState["ImgPath"].ToString() + "','" + ViewState["ImgView"].ToString() + "')"; } else if (strAction == "update") { sqlStr = "update S_NewsInfo set N_Title='" + txtTitle.Text.Trim() + "',N_Content='" + strContent + "',N_From='" + txtFrom.Text.Trim() + "',N_Date='" + txtDate.Text.Trim() + "',N_ImgPath='" + ViewState["ImgPath"].ToString() + "',N_ImgView='" + ViewState["ImgView"].ToString() + "' where N_ID='" + ViewState["nid"].ToString() + "'"; } if (DBHelper.ExecuteCommand(sqlStr) > 0) { Response.Redirect(((DataTable)ViewState["dtType"]).Rows[0]["SelectURL"].ToString()); } }