HttpFileCollection = Request.Files

The Files collection, which is new to ASP.NET, returns a collection of type HttpFileCollection that contains any files uploaded by the user’s current request. This collection is especially useful in combination with the HtmlInputFile Server Control, which provides the basic plumbing necessary to upload files via an HTTP POST request. When a user submits one or more files (one per HtmlInputFile control on the submitting page), you can retrieve the files by using the Files collection.



An Object variable of type HttpFileCollection.


The example uses two HtmlInputFile server controls and a server-side <script> block to upload files and process them. The example shows both the <form> section of the page and its controls and the <script> block containing the UploadBtn_OnClick method called by the onServerClick event of the HtmlInputButton control:

<!--Place between the <head> and </head> tags --> <script runat="server"> Sub UploadBtn_Click(Sender as Object, e as EventArgs) UploadForm.Visible = False If InStr(Request.ContentType, "multipart/form-data") Then Dim Counter1 As Integer Dim Keys( ) As String Dim Files As HttpFileCollection ' Load File collection Files = Request.Files ' Get names of all files into an array Keys = Files.AllKeys For Counter1 = 0 To Keys.GetUpperBound(0) Message.Text &= "File ID: " & Keys(Counter1) & "<br/>" Message.Text &= "File Name/Path: " & _ Files(Counter1).FileName & "<br/>" ...

Get ASP.NET in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.