﻿ FileUpload Web Server Control Overview

JavaScript Editor jscript editor     Web designer ﻿

Main Page


Prompting for Files to Upload


Handling Uploaded Files


You can access the uploaded file in the following ways:

  • As a byte array exposed in the FileUpload controlвЂ™s FileBytes property.

  • As a stream exposed in the FileContent property.

  • As an object of type HttpPostedFile in the PostedFile property. The PostedFile object exposes properties, such as the ContentType and ContentLength properties, which provide you with information about the uploaded file.

When your code runs, you can examine the characteristics of the file, such as its name, size, and MIME type, and you can then save it. You can work with the file as a byte array or stream. Alternatively, both the FileUpload control and the HttpPostedFile object support a SaveAs method that writes the file to disk.

There is no inherent limitation on where you can save uploaded files. However, to save the file, the ASP.NET process must have permission to create files in the location that you specify. In addition, your application might be configured to require an absolute path (not a relative path) for saving the file, which is a security measure. If the requireRootedSaveAsPath attribute of the httpRuntime Element (ASP.NET Settings Schema) configuration element is set to true (which is the default), you must provide an absolute path when saving the uploaded file.


You can create an absolute path based on the root of your application by using the MapPath method of the HttpServerUtility class and passing to the method the tilde (~) operator, which represents the application root folder. For more information, see ASP.NET Web Site Paths.


Security and the FileUpload Control



Before the page is submitted, you can use client script to examine the file name that a user has typed in the text box. However, while performing a client-side check of the file name can be useful, it does not guarantee that users cannot upload an unsafe file type, such as an executable file.

See Also

JavaScript Editor jscript editor     Web designer ﻿