protected void btnupload2_Click(object sender, EventArgs e) { if (FileUploadupdate.HasFile) { string datatime = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".jpg"; string webfilepath = Server.MapPath("~/staff_images/" + datatime); string filecontent_type = FileUploadupdate.PostedFile.ContentType; if (filecontent_type == "image/bmp" || filecontent_type == "image/gif" || filecontent_type == "image/jpeg" || filecontent_type == "image/jpg" || filecontent_type == "image/pjpeg") { if (!File.Exists(webfilepath)) { try { FileUploadupdate.SaveAs(webfilepath); lblupdate.Visible = true; lblupdate.Text = "上传成功"; showImage.ImageUrl = "~/staff_images/" + datatime; SqlCommand sqlCmd = new SqlCommand(); sqlConn.ConnectionString = DBCallCommon.GetStringValue("connectionStrings"); sqlCmd.CommandText = "update TBDS_STAFFINFO set JPGURL=@filename where ST_NAME=" + "'" + ST_NAME.Text + "'"; sqlCmd.Parameters.Clear(); sqlCmd.Parameters.AddWithValue("@filename", datatime); sqlCmd.Connection = sqlConn; sqlConn.Open(); sqlCmd.ExecuteNonQuery(); sqlConn.Close(); } catch (Exception ex) { lblupload2.Visible = true; lblupload2.Text = "文件上传失败" + ex.Message; } } else { lblupload2.Visible = true; lblupload2.Text = "文件已经存在,请重新命名"; } } else { lblupload2.Visible = true; lblupload2.Text = "文件类型不符,请重新上传"; } } else { lblupload2.Visible = true; lblupload2.Text = "请选择文件或输入文件路径及名称"; } }
protected void btnupload_Click(object sender, EventArgs e)//上传功能 { #region 现在的 string time1 = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string FilePath = @"D:\数字平台\平台代码\TS_ZCZJ_DPF(2015-07-06)\ZCZJ_DPF\staff_images"; if (!Directory.Exists(FilePath))//如果不存在文件路径就创建一个 { Response.Write("<script>alert('不存在此文件夹!!!请与管理员联系!!!')</script>"); return; } //对客户端已上载的单独文件的访问 HttpPostedFile UserHPF = FileUploadupdate.PostedFile; try { string fileContentType = UserHPF.ContentType; // 获取客户端发送的文件的 MIME 内容类型 if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/jpeg" || fileContentType == "image/jpg" || fileContentType == "image/pjpeg") //传送文件类型 { if (UserHPF.ContentLength > 0) { //调用GetAutoID方法获取上传文件自动编号 //int IntFieldID = CC.GetAutoID("fileID", "tb_files"); //文件的真实名(格式:[文件编号]上传文件名) //用于实现上传多个相同文件时,原有文件不被覆盖 string strOldFile = System.IO.Path.GetFileName(UserHPF.FileName); string strExtent = strOldFile.Substring(strOldFile.LastIndexOf(".")); string strNewFile = time1 + strExtent; if (!File.Exists(FilePath + "//" + strNewFile)) { //定义插入字符串,将上传文件信息保存在数据库中 if (Request.QueryString["action"] != "add") { string st_id = Request.QueryString["ST_ID"].ToString(); string sql = string.Format("update TBDS_STAFFINFO set JPGURL='{0}' where ST_ID={1}", time1, st_id); DBCallCommon.ExeSqlText(sql); } UserHPF.SaveAs(FilePath + "//" + strNewFile);//将上传的文件存放在指定的文件夹中 } else { lblupload.Visible = true; lblupload.Text = "文件名与服务器某个合同名重名,请您核对后重新上传!"; } } } else { lblupload.Visible = true; lblupload.Text = "文件类型不符合要求,请您核对后重新上传!"; } } catch { lblupload.Text = "文件上传过程中出现错误!"; lblupload.Visible = true; return; } if (FileUploadupdate.HasFile) { string datatime = time1 + ".jpg"; string webfilepath = Server.MapPath("~/staff_images/" + datatime); string filecontent_type = FileUploadupdate.PostedFile.ContentType; if (filecontent_type == "image/bmp" || filecontent_type == "image/gif" || filecontent_type == "image/jpeg" || filecontent_type == "image/jpg" || filecontent_type == "image/pjpeg") { if (!File.Exists(webfilepath)) { try { FileUploadupdate.SaveAs(webfilepath); lblupload.Visible = true; lblupload.Text = "上传成功"; showImage.ImageUrl = "~/staff_images/" + datatime; if (Request.QueryString["action"] != "add")//也可用DBCallmand.Excyte。 { string st_id = Request.QueryString["ST_ID"].ToString(); SqlCommand sqlCmd = new SqlCommand(); sqlConn.ConnectionString = DBCallCommon.GetStringValue("connectionStrings"); sqlCmd.CommandText = "update TBDS_STAFFINFO set JPGURL=@filename where ST_ID=" + "'" + st_id + "'"; sqlCmd.Parameters.Clear(); sqlCmd.Parameters.AddWithValue("@filename", datatime); sqlCmd.Connection = sqlConn; sqlConn.Open(); sqlCmd.ExecuteNonQuery(); sqlConn.Close(); } else { JPG.Visible = false; JPG.Text = datatime; } } catch (Exception ex) { lblupload.Visible = true; lblupload.Text = "文件上传失败" + ex.Message; } } else { lblupload.Visible = true; lblupload.Text = "文件已经存在,重新命名!"; } } else { lblupload.Visible = true; lblupload.Text = "文件类型不符,重新上传!"; } } else { lblupload.Visible = true; lblupload.Text = "请选择文件!"; } #endregion #region 原来的 //if (FileUploadupdate.HasFile) //{ // string datatime = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".jpg"; // string webfilepath = Server.MapPath("~/staff_images/" + datatime); // string filecontent_type = FileUploadupdate.PostedFile.ContentType; // if (filecontent_type == "image/bmp" || filecontent_type == "image/gif" || filecontent_type == "image/jpeg" || filecontent_type == "image/jpg" || filecontent_type == "image/pjpeg") // { // if (!File.Exists(webfilepath)) // { // try // { // FileUploadupdate.SaveAs(webfilepath); // lblupload.Visible = true; // lblupload.Text = "上传成功"; // showImage.ImageUrl = "~/staff_images/" + datatime; // if (Request.QueryString["action"] != "add")//也可用DBCallmand.Excyte。 // { // string st_id = Request.QueryString["ST_ID"].ToString(); // SqlCommand sqlCmd = new SqlCommand(); // sqlConn.ConnectionString = DBCallCommon.GetStringValue("connectionStrings"); // sqlCmd.CommandText = "update TBDS_STAFFINFO set JPGURL=@filename where ST_ID=" + "'" + st_id + "'"; // sqlCmd.Parameters.Clear(); // sqlCmd.Parameters.AddWithValue("@filename", datatime); // sqlCmd.Connection = sqlConn; // sqlConn.Open(); // sqlCmd.ExecuteNonQuery(); // sqlConn.Close(); // } // else // { // JPG.Visible = false; // JPG.Text = datatime; // } // } // catch (Exception ex) // { // lblupload.Visible = true; // lblupload.Text = "文件上传失败" + ex.Message; // } // } // else // { // lblupload.Visible = true; // lblupload.Text = "文件已经存在,重新命名!"; // } // } // else // { // lblupload.Visible = true; // lblupload.Text = "文件类型不符,重新上传!"; // } //} //else //{ // lblupload.Visible = true; // lblupload.Text = "请选择文件!"; //} #endregion }