FileUpload Control

Often an application finds the need to allow users to upload files to the web server. Although it was possible to do this in ASP.NET Version 1.x, it is made much easier in ASP.NET Version 2 with the introduction of the FileUpload control.

This control makes it easy for the user to browse for and select the file to transfer, providing a Browse button and a text box for entering the file name. Once the user has entered a fully-qualified file name in the text box, either by typing it directly or using the Browse button, the SaveAs method of the FileUpload control can be called to save the file to disk.

In addition to the normal complement of members inherited from the WebControl class, the FileUpload control also exposes several read-only properties of particular interest, listed in Tables 5-8 and 5-9.

Table 5-8. FileUpload properties

Name

Type

Get

Set

Description

FileContent

Stream

 

Returns a Stream object that points to the file to upload.

FileName

string

 

Returns the name of the file to be uploaded, without any qualifying path information.

HasFile

Boolean

 

If true, indicates that the control has a file to upload.

PostedFile

HttpPostedFile

 

Returns a reference to the file which has been uploaded. Exposes the read-only properties listed in Table 5-9.

Table 5-8. HttpPostedFile properties

Name

Type

Get

Set

Description

ContentLength

integer

 

Returns the size of the file, in bytes, of an uploaded file. ...

Get Programming ASP.NET, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.