using System;
using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Net; using System.Text.RegularExpressions; using System.Data.SqlClient; public partial class ImgUpdata : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { this.Image1.ImageUrl = "ImgUpload/null.gif"; this.FileUpload1.Attributes.Add("onchange", "document.getElementById('Image1').src=document.getElementById('" + this.FileUpload1.ClientID + "').value;"); //图片预览 //打开数据连接 //SqlConnection cn = sqldata.createcon(); //cn.Open(); } protected void Button1_Click(object sender, EventArgs e) { bool yesno = false; bool rename = false; if (FileUpload1.HasFile) { string fileContentType = FileUpload1.PostedFile.ContentType; if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg") { string newname = "051011102";//session["userid"]; string name = FileUpload1.FileName; // 获取文件名称 string[] kzm = name.Split('.'); string webFilePath = Server.MapPath("ImgUpload/" + name); // 服务器端文件路径 string newwebFilePath = Server.MapPath("ImgUpload/" + newname+"."+kzm[1]); if (!File.Exists(webFilePath)) { try { FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件 Label1.Text = "文件上传成功"; yesno = true; } catch (Exception ex) { Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message; } } else { Label1.Text = "提示:文件已经存在,请重命名后上传"; } if (yesno == true) { try { File.Move(webFilePath, newwebFilePath); rename = true; } catch (Exception ex) { Response.Write("<script>alert('文件上传错误')</script>"); } } } else { Label1.Text = "提示:文件类型不符"; } } if (rename == true) { //Response.Write("对了"); //updata数据库,并关闭数据库连接 //SqlCommand cmd = new SqlCommand("updata ……", cn); //cn.Close(); } } } |