ASP.NET文件上传

    技术2022-05-19  26

      //普通文件的上传

            protected void btnUpload_Click(object sender, EventArgs e)

            {

                string fullFileName = this.FileUpload1.PostedFile.FileName;

                string fileName = fullFileName.Substring(fullFileName.LastIndexOf("//") + 1);

                this.FileUpload1.PostedFile.SaveAs(Server.MapPath("upload") + "//" + fileName);

            }

     

            //图片的上传

            protected void btnUploadImage_Click(object sender, EventArgs e)

            {

                string fullFileName = this.FileUpload2.PostedFile.FileName;

                string fileName = fullFileName.Substring(fullFileName.LastIndexOf("//") + 1);

                string fileType = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);

                if(fileType == "gif" || fileType == "jpg" || fileType == "png")

                {

                    this.FileUpload2.PostedFile.SaveAs(Server.MapPath("upload") + "//" + fileName);

                    Response.Write("上传成功");

                }

                else

                {

                    Response.Write("<script language='javascript'>alert('您的上传文件有误!!!');</script>");

                }

            }


    最新回复(0)