关于FileUpload控件的二种用法,你都懂吗?

      判断是上传的是否是.JPG图片

              JS代码

       <script type="text/javascript">
            function chkPhoto(fnUpload)
             {
                 var filename = fnUpload.value;
                 var mime = filename.toLowerCase().substr(filename.lastIndexOf("."));
                 if (mime!=".jpg")
                  {
                     alert("请选择JPG格式的图片!");
                     fnUpload.outerHTML=fnUpload.outerHTML;               
                  }
             }
       </script>
                

             标签代码  注意  onchage事件

                       <asp:FileUpload ID="fnPhoto" runat="Server"  onchage="chkPhoto(this)" />

           为Image控件设定默认图片 和出错时的图片

<asp:Image ID="Image1" runat="server" ImageUrl="~/image/HR201234170434507500.jpg" Width="180px" Height="200px" onerror="this.src='/webJPG/image/HR201234170434507500.jpg'"/>

    关于上传按钮代码

  protected void Button1_Click(object sender, EventArgs e)
    {
        string ConStr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
        string str = "HR"+DateTime.Now.ToString("yyyymmddhhmmssffff");
        Response.Write(str);
        string path = "~/image/" + str + ".jpg";
        string sql = string.Format("insert into tb_image(image) values('{0}')", path);
        SqlConnection scon = new SqlConnection(ConStr);
        scon.Open();
        SqlCommand cmd = new SqlCommand(sql, scon);
        try
        {
            int i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), null, "alert('添加成功!')", true);
                if (FileUpload1.FileName != "")
                {
                    FileUpload1.SaveAs(Server.MapPath(path));
                    this.Image1.ImageUrl = path;
                }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), null, "alert('添加失败!')", true);
            }
        }
        catch (Exception ex)
        {
           
            throw new Exception(ex.Message);
        }
    }

先添加using System.IO;命名空间

protected void Button1_Click(object sender, EventArgs e)         //方法一
{
  //上传文件到load下的upload文件夹
  FileUpload1.SaveAs(Server.MapPath("load/upload"+"\\"+Path.GetFileName(FileUpload1.PostedFile.FileName)));
  //获取上传文件的名称
  Response.Write(FileUpload1.PostedFile.FileName+"<br>");
  //获取上传文件的类型
  Response.Write(FileUpload1.PostedFile.ContentType+"<br>");
  //获取上传文件的大小
  Response.Write(FileUpload1.PostedFile.ContentLength);
  string strpath=FileUpload1.PostedFile.FileName;
  //获取文件的扩展名
  Response.Write(strpath.Substring(strpath.LastIndexOf(".") + 1));

}
protected void Button2_Click(object sender, EventArgs e)           //方法二
{
  HttpPostedFile pf = this.FileUpload1.PostedFile;
  //获取上传文件的完整路径
  Response.Write("上传文件路径为::"+pf.FileName+"<br>");
  //获取上传文件的类型
  Response.Write("上传文件的类型为:" + Path.GetExtension(pf.FileName)+"<br>");
  //获取上传文件的大小
  Response.Write(pf.ContentLength.ToString());
  //上传文件到load文件夹下的upload文件夹
  pf.SaveAs(Server.MapPath("load/upload" + "\\" + Path.GetFileName(pf.FileName)));
}

posted @ 2012-04-14 00:19  酒沉吟  阅读(943)  评论(0编辑  收藏  举报