FileUpload的文件上传

来源:互联网 发布:java猎魔人下载 编辑:程序博客网 时间:2024/05/16 01:07

 protected void Button1_Click(object sender, EventArgs e)
    {
        Boolean fileOK = false;
        String path = Server.MapPath("~/uploadFile/");
        if (FileUpload1.HasFile)
        {
            String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions ={ ".gif",".mp3",".jpg",".txt",".doc"};
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;

                }
            }
        }
        if (fileOK)
        {
                FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
                try
                {
                    //SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sasa;database=wld");
                    //con.Open();
                    //'"+path+"'+
                    //string sqlstr = "insert into image_tb(image,name) values('"+FileUpload1.FileName+"',4)";
                    //SqlCommand com = new SqlCommand(sqlstr, con);
                    //com.ExecuteNonQuery();
                    Response.Write("上传成功!");
                }
                catch (Exception ex)
                {
                    Response.Write("异常");
                }
        }
        else
        {
            Response.Write("文件类型不对!");
        }

         
    }

原创粉丝点击