Validating file upload control

Rated 3.88/5 based on 772 customer reviews

The File Upload class displays a text box control and a browse button that enable users to select a file on the client and upload it to the Web server.

The user specifies the file to upload by entering the full path of the file on the local computer (for example, C:\My Files\Test File.txt) in the text box of the control.

This example allows only files that have the or file name extensions to be uploaded. Get Extension method is called to return the extension of the file to upload.

If the file has a or file name extension, the Save As method is called to save the file to the specified path on the server.

The Content Type property gets the MIME content type of the file.

In addition, you can use the Posted File property to access the File Name property, the Input Stream property, and the Save As method.

Caution These examples demonstrate the basic syntax for the File Upload control, but they do not demonstrate all the necessary error checking that should be completed before to saving the file. The following example demonstrates how to create a File Upload control that saves files to a path that is specified in code.

The Save As method is called to save the file to the specified path on the server.

validating file upload control-4

For example, you can provide a button that the user clicks to upload the file.Lets take a look at this Demo first : This file upload control will only take zip or png files which are less than 1MB in size. Javascript Data attributes are used to specify the allowed file types and the maximum size of the upload file. So allowed file types are zip and png , and allowed maximum size is 1000000 bytes , that is nearly 1 MB.The file name that this property returns does not include the path of the file on the client.The File Content property gets a Stream object that points to a file to upload.

Leave a Reply