July 30, 2009

Using FileUpload control

Asp.net provides a FileUpload control that allows user to upload file to the server. The FileUpload control is represented as <asp:FileUpload> element. The control does not post back to the server and therefore a different control, like Button, is necessary to cause the postback.

In this example, I will use a FileUpload control to save a file to the server. Here is the main page.



The page contains a FileUpload and a Button control.
Here, is the code behind for the page.


The filename, size and the mime type of the file can be found using FileName property, Posted.ContentLength and PostedFile.ContentType properties respectively. The file can be saved using the SaveAs method passing the full path where the file will be saved to. Note that the user running the application will need write access to directory where the file will be saved.

The FileUpload control can save a maximum size as specified by the maxRequestLength attribute of the httpRuntime element in the web.config file. The default is 4MB. To modify the settings change or add the httpRuntime element in the web.config file as below.



1 comments:

mahi said...

Hey Guys ,
I m new to this blog its very interesting to share the their knowledge ,

I have one doubt

I hv to create some images with text coming from the database and add some onclicks for that as well ..

can u guys give me some inputs for me .

Thanks
Mahesh