追逐梦想的猪

人生因为有梦想而精彩!

导航

在ASP.Net中如何实现电子照片的上传

目前,在很多的应用中,都有碰到上传电子照片或者图片的情况,那么就来看一下,在.Net中是如何实现这个功能的。

//上传电子照片,没有保存到数据库,只是暂存在Session中,并在image控件上显示
  private void btn_upload_Click(object sender, System.EventArgs e)
  {
   //每次上传照片之前,先清空图片Session["imageData"],Session["imageSize"]和Session["tempImage"]中的内容
   Session["imageData"] = null;
   Session["imageSize"] = null;
   Session["tempImage"] = null;

   HttpPostedFile upFile = this.up_file.PostedFile;
   int iFileLength = upFile.ContentLength;
   string imageType;
   try
   {
    if( upFile.FileName.Equals("") )
    {
     imageType = "";
    }
    else
    {
     imageType = upFile.FileName.Substring( upFile.FileName.LastIndexOf('.'), upFile.FileName.Length - upFile.FileName.LastIndexOf('.') ).Trim().ToLower();
    }
    if(iFileLength == 0)
    {
     Response.Write("<script language=javascript>");
     Response.Write("alert('请先选择要上传的文件!');");
     Response.Write("</script>");
     return;
    }
    else if( iFileLength > 1048576)
    {
     Response.Write("<script language=javascript>");
     Response.Write("alert('上传文件太大,系统限制最大为1M!');");
     Response.Write("</script>");
     return;
    }
    else if(this.tb_gh.Text == "")
    {
     Response.Write("<script language=javascript>");
     Response.Write("alert('请先填写工号!');");
     Response.Write("</script>");
     return;
    }
    else if( imageType != ".jpg" && imageType != ".bmp" && imageType != ".gif")
    {
     Response.Write("<script language=javascript>");
     Response.Write("alert('图片格式不正确,要求jpg格式的图片!');");
     Response.Write("</script>");
     return;
    }
    else
    {
     this.Image1.ImageUrl = this.up_file.PostedFile.FileName;

     if(this.Image1.Width.Value > 100 || this.Image1.Height.Value > 120 )
     {
      this.Image1.ImageUrl = "";
      Response.Write("<script language=javascript>");
      Response.Write("alert('上传的照片尺寸太大!尺寸限制在100*120之内!');");
      Response.Write("</script>");
      return;
     }
     else
     {
      this.lb_msg.Visible = true;
      this.lb_msg.Text = "电子照片上传成功!";
    
      byte[] FileByteArray = new byte[iFileLength];
      Stream StreamObject = upFile.InputStream;
      int ImgSize = upFile.ContentLength;
      string ImgType = upFile.ContentType;
      string ImgID = this.tb_gh.Text.Trim();

      Session["tempImage"] = (HttpPostedFile)upFile;

      //重新将图片信息赋值给Session
      Session["imageData"] = (byte [])FileByteArray;
      Session["imageSize"] = Convert.ToInt32(ImgSize.ToString());
     }
     
    }
   }
   catch(Exception ex)
   {
    this.lb_msg.Visible = true;
    this.lb_msg.Text = ex.Message.ToString();
   }
  }
本例中,是把图片以二进制byte []的形式存入数据库;取出时,先读取二进制流,然后再写入一个temp.jpg文件中进行显示,代码如下:

    //创建一个temp.jpg文件,若已经存在,则改写该文件为空文件
    FileStream fs = new FileStream(Server.MapPath("..\\image\\temp.jpg"), FileMode.Create);
     fs.Write( (byte [])read["lzb2dzzp"], 0, Convert.ToInt32(read["lzb2imageSize"]) );
    this.Image1.ImageUrl = "..\\image\\temp.jpg";
    
    fs.Close();    
Session["imageData"] = (byte [])read["lzb2dzzp"];
    Session["imageSize"] = Convert.ToInt32( read["lzb2imageSize"] );


也可以选择将图片存入文件目录中,在读取和存储效率方面,可能会更好一些。

存在的问题是:假如第一次查询数据库(或者目录),选择显示A的照片,可以正常显示在image上;第二次,重新进行查询,然后选择显示B的照片,则显示的仍是A的照片,需要刷新一次才可以正常显示B的照片;该问题,自己还未能解决,可能是与缓存有关,但并不清楚具体问题出在哪里。若有谁可以提供一个好的解决方案,希望可以在此留言。

posted on 2005-12-06 16:07  Ann  阅读(1012)  评论(1编辑  收藏  举报