效果:
思路:
首先,获取图片物理地址,然后进行判断将图片保存到文件夹下,再将图片的信息保存到数据库。
数据库:
1 create table image12 (3 ID int identity(1,1) primary key,4 ImageName varchar(100) ,5 ImageType varchar(20),6 ImagePath varchar(200)7 )
代码:
1 229
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.IO; 8 using System.Configuration; 9 using System.Data;10 using System.Data.SqlClient;11 12 namespace InExcelOutExcel13 {14 public partial class UpWord : System.Web.UI.Page15 {16 protected void Page_Load(object sender, EventArgs e)17 {18 19 }20 string SQLString = ConfigurationManager.ConnectionStrings["ConnectionStr"].ToString();21 protected void UploadButton_Click(object sender, EventArgs e)22 {23 try24 {25 using (SqlConnection sqlcon = new SqlConnection(SQLString))26 {27 string FullName = FileUpload1.PostedFile.FileName;//获取图片物理地址28 FileInfo fi = new FileInfo(FullName);29 string name = fi.Name;//获取图片名称30 string type = fi.Extension;//获取图片类型31 if (type == ".jpg" || type == ".gif" || type == ".bmp" || type == ".png")32 {33 string SavePath = Server.MapPath("~\\excel");//图片保存到文件夹下34 this.FileUpload1.PostedFile.SaveAs(SavePath + "\\" + name);//保存路径35 this.Image1.Visible = true;36 this.Image1.ImageUrl = "~\\excel" + "\\" + name;//界面显示图片37 string sql = "insert into image1(ImageName,ImageType,ImagePath) values('" + name + "','" + type + "','~\\excel" + name + "')";38 SqlCommand cmd = new SqlCommand(sql, sqlcon);39 sqlcon.Open();40 cmd.ExecuteNonQuery();41 this.label1.Text = "上传成功";42 }43 else44 {45 this.label1.Text = "请选择正确的格式图片";46 }47 }48 }49 catch (Exception ex)50 {51 Response.Write(ex.Message);52 }53 }54 }55 }