上传文件并建立相对应的文件夹
1
if (FileUp.PostedFile != null)2

{3
String filename = FileUp.FileName;4
int i = filename.LastIndexOf(".");5
String lastname = filename.Substring(i);6
if ( lastname != ".jpg")7

{8
Response.Write("文件格式不正确!");9
Response.End();10
}11
DateTime now = DateTime.Now;12
String classid = DateTime.Now.ToString("yyyy-MM-dd");13
if (!Directory.Exists(HttpContext.Current.Server.MapPath("upload/") + "\\" + classid))14

{15
System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("upload/") + "\\" + classid);16
}17
String upfilename = now.Millisecond.ToString() + "_" + FileUp.PostedFile.ContentLength.ToString() + lastname;18
FileUp.PostedFile.SaveAs(Server.MapPath("upload\\" + classid + "\\" + upfilename));19
20
String imgurl = "upload\\" + classid + "\\" + upfilename;21
String shopid = ddl_shop.SelectedValue;22
String connstr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;23
SqlConnection con = new SqlConnection(connstr);24
con.Open();25
SqlCommand comm = new SqlCommand();26
comm.Connection = con;27
comm.CommandText = "insert into tg163k_shopyh(shopid,[image])values(" + shopid + ",'" + imgurl + "')";28
int j = comm.ExecuteNonQuery();29
if (j>0)30

{31
Response.Write("<script>alert('添加成功');location.href='Default.aspx';</script>");32

33
} 34
}
浙公网安备 33010602011771号