2019独角兽企业重金招聘Python工程师标准>>>  
FileUpload控件的主要中能:向指定目录上传文件,该控件包括一个文本框和一个浏览按钮。
常用的属性:FileBytes,FileContent、FileName、HasFile、PostedFile。
常用的方法:核心:SaveAs(String filename), HasFile 的属性ture 和false。
首先在HTML中添加以下代码:加载基本控件
<body>
</div> 第二个练习 
<div>
 <asp:FileUpload ID="Fileload2" runat="server" style="z-index : 1;" />
 <asp:Button ID="确定" runat="server" BorderStyle ="NotSet" Height ="20px" Width ="85px" />
 </div>
 <div>
 <asp:Label ID="Text1" runat ="server" BorderStyle ="NotSet" Height ="20px" Width="85px"></asp:Label>
 <br />
 <asp:Label ID="Text2" runat ="server" Height ="20px" Width="85px"></asp:Label>
 <br />
 <asp:Label ID="Text3" runat ="server" Height ="20px" Width="85px"></asp:Label>
 <br />
 <asp:Label ID="Text4" runat ="server" Height ="20px" Width="85px"></asp:Label>
 </div>
在cs文件中添加
 protected void 上传_Click(object sender, EventArgs e)
 {
 bool fileValid = false;
 //如果确认了上传文件,则判断文件类型是否符合要求
 if(this.Fileload2.HasFile )
 {
 //获取上传文件的后缀
 String fileExtrension = System.IO.Path.GetExtension(this.Fileload2.FileName).ToLower();
 String[] restritExension = { ".gif", ".jpg", ".bmp", "png" };
 //判断文件类型是否符合要求
 for(int i=0;i<restritExension.Length;i++)
 {
 if(fileExtrension ==restritExension[i])
 {
 fileValid = true;
 }
 }
 }
 //如果文件类型符合要求,调用SaveAs方法实现上传,并显示相关信息
 if(fileValid ==true )
 {
 try
 {
 this.image1.ImageUrl = "" + Fileload2.FileName;
 this.Fileload2.SaveAs(Server.MapPath("") + Fileload2.FileName);
 this.Text1.Text = "文件长传成功";
 this.Text2.Text += "<li>" + "源文件路径:" + this.Fileload2.PostedFile.FileName;
 this.Text3.Text += "<li>" + "文件大小:" + this.Fileload2.PostedFile.ContentLength + "字节";
 this.Text4.Text += "<li>" + "文件类型:" + this.Fileload2.PostedFile.ContentType;
 }
 catch
 {
 this.Text1.Text = "文件上传不成功!";
 }
 finally { }
 }
 else
 {
 this.Text1.Text = "只能够上传后缀为Gif,jpg,bmp,png的文件";
 }
 }