ASP.NET2.0中FileUpload控件的使用(即ASP.NET2.0中如何上传文件)

时间:2022-11-17 07:39:55

第一步:新建一个FileUpload.aspx页面。

第二步:在FileUpload.aspx页面中放入FileUpload控件,Button控件及Label控件[用于显示上传文件的相关信息,如类型,大小等]

第三步:在D盘上建一文件夹upload,并设置其权限为可读可写[具体设置请自行参考其它书籍]。

第四步:编程。在页面上双击Button控件,编写Button1_Click事件,程序如下:

    protected void Button1_Click(object sender, EventArgs e)

    {

        if (FileUpload1.HasFile)

        {

            try

            {

                FileUpload1.SaveAs("D://upload//" + FileUpload1.FileName);

                Label1.Text = "File Name: " +

                    FileUpload1.PostedFile.FileName + "<br>" +

                    FileUpload1.PostedFile.ContentLength + "kb<br>" +

                    "Content Type: " +

                    FileUpload1.PostedFile.ContentType;

            }

            catch (Exception ex)

            {

                Label1.Text = "ERROR: " + ex.Message.ToString();

            }

        }

        else

        {

            Label1.Text = "You have not specified a file.";

        }

    }

第五步:浏览,测试