DotNet2.0中上传文件

ASP.NET 2.0 提供新 FileUpload 服务器控件来上传文件。

 

 1 <% @ Page Language = " C# "   %>
 2 < script runat = " server " >
 3      protected   void  Button1_Click( object  sender, EventArgs e)
 4      {
 5        if (FileUpload1.HasFile)
 6            try
 7            {
 8                FileUpload1.SaveAs("C:\\Uploads\\" + 
 9                     FileUpload1.FileName);
10                Label1.Text = "File name: " +
11                     FileUpload1.PostedFile.FileName + "<br>" +
12                     FileUpload1.PostedFile.ContentLength + " kb<br>" +
13                     "Content type: " +
14                     FileUpload1.PostedFile.ContentType;
15            }

16            catch (Exception ex)
17            {
18                Label1.Text = "ERROR: " + ex.Message.ToString();
19            }

20        else
21        {
22            Label1.Text = "You have not specified a file.";
23        }

24    }

25 </ script >
26
27 < HTML xmlns = " http://www.w3.org/1999/xHTML "   >
28 < head runat = " server " >
29      < title > Upload Files </ title >
30 </ head >
31 < body >
32      < form id = " form1 "  runat = " server " >
33      < div >
34          < ASP:FileUpload ID = " FileUpload1 "  runat = " server "   />< br  />
35          < br  />
36          < ASP:Button ID = " Button1 "  runat = " server "  OnClick = " Button1_Click "  
37          Text = " Upload File "   />   < br  />
38          < br  />
39          < ASP:Label ID = " Label1 "  runat = " server " ></ ASP:Label ></ div >
40      </ form >
41 </ body >
42 </ HTML >
43

使用验证控件限制上载到服务器的文件类型

 

< ASP:FileUpload ID = " FileUpload1 "  runat = " server "   />< br  />
< br  />
< ASP:Button ID = " Button1 "  runat = " server "  OnClick = " Button1_Click "  
 Text
= " Upload File "   />   < br  />
< br  />
< ASP:Label ID = " Label1 "  runat = " server " ></ ASP:Label >
< ASP:RegularExpressionValidator 
 id
= " RegularExpressionValidator1 "  runat = " server "  
 ErrorMessage
= " Only mp3, m3u or mpeg files are allowed! "  
 ValidationExpression
= " ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))
     + (.mp3 | .MP3 | .mpeg | .MPEG | .m3u | .M3U)$ "  
 ControlToValidate = " FileUpload1 " ></ ASP:RegularExpressionValidator >
< br  />
< ASP:RequiredFieldValidator 
 id
= " RequiredFieldValidator1 "  runat = " server "  
 ErrorMessage
= " This is a required field! "  
 ControlToValidate
= " FileUpload1 " ></ ASP:RequiredFieldValidator >


 

同时上传多个文件

 

 1 protected   void  Button1_Click( object  sender, EventArgs e)
 2 {
 3   string filepath = "C:\\Uploads";
 4   HttpFileCollection uploadedFiles = Request.Files;
 5    
 6   for (int i = 0; i < uploadedFiles.Count; i++)
 7   {    
 8      HttpPostedFile userPostedFile = uploadedFiles[i];
 9    
10      try
11      {    
12         if (userPostedFile.ContentLength > 0 )
13         {
14            Label1.Text += "File #" + (i+1+ 
15               "";
16            Label1.Text += "File Content Type: " + 
17               userPostedFile.ContentType + "";
18            Label1.Text += "File Size: " + 
19               userPostedFile.ContentLength + "kb";
20            Label1.Text += "File Name: " + 
21               userPostedFile.FileName + "";
22    
23            userPostedFile.SaveAs(filepath + "\\" + 
24               System.IO.Path.GetFileName(userPostedFile.FileName));
25    
26            Label1.Text += "Location where saved: " + 
27               filepath + "\\" + 
28               System.IO.Path.GetFileName(userPostedFile.FileName) + 
29               "
30";
31         }
    
32      }
 
33      catch (Exception Ex)
34      {    
35         Label1.Text += "Error: " + Ex.Message;    
36      }
    
37   }
    
38}

39
40

你可能感兴趣的:(上传文件)