日期:2014-05-18  浏览次数:20427 次

菜鸟~请教图片上传问题~~
protected void btnfileup_Click(object sender, EventArgs e)//上传图片
  {
  string strext;
  int filelength;
  string strName;
  int intext;
  string struppath;
  if (fileimg.PostedFile.ContentLength > 0)
  {
  try
  {
  filelength = fileimg.PostedFile.ContentLength;
  strName = fileimg.PostedFile.FileName;
  intext = strName.LastIndexOf(".");
  strext = strName.Substring(intext);
  System.Drawing.Image image;
  if (filelength > 3000000)
  {
  Response.Write("<script>alert('图片大于300K,无法上传')</script>");
  }
  else
  {
   
  struppath = @"..\img\";
  if (strext == ".jpg" || strext == ".gif")
  {
  if (!System.IO.Directory.Exists(Server.MapPath(struppath)))
  {
  System.IO.Directory.CreateDirectory(Server.MapPath(struppath));
  }
  string strurl = Server.MapPath(struppath+@"\"+fileimg.FileName);
  fileimg.SaveAs(strurl);
  image = System.Drawing.Image.FromFile(strurl);
  image.Dispose();
  Image1.ImageUrl = @"..\img\" + fileimg.FileName;
   
  //Response.Write("<script>alert('上传成功!')</script>");
   
  }
   
  else
  {
  Response.Write("<script>alert('图片上传失败!格式必须为.jpg或.gif')</script>");
  }
  fileimg.SaveAs(Server.MapPath(fileimg.FileName));
  }
  }
  catch (Exception ex)
  {
  Response.Write(ex.Message);
  Response.Write("<script>alert('" + ex + "')</script>");
  }
  }
  else 
  {
  Response.Write("<script>alert('请先选择上传图片的路径!')<script>");
  }

  }
  protected void btnadd_Click(object sender, EventArgs e)//添加进数据库
  {
  string title = TextBox1.Text;
  string count = TextBox2.Text;
  string pic = Server.MapPath(fileimg.FileName);

  string sql = "insert into Commentary valuse('" + title + "','" + count + "','" + pic + "')";
  SqlCommand cmd = new SqlCommand(sql,conn);
  try
  {
  conn.Open();
  cmd.ExecuteNonQuery();
  Response.Redirect("~/adminAdd/Ok.aspx");
  }
  catch (Exception ex)
  {
  Response.Write("<script>alert('添加失败!')</script>");
  Response.Write("<script>alert('" + ex.Message + "')</script&