Upload Files in ASP.NET
At times you may need your client send some data to the server which is contained in a file (any document), for this to happen, you need the file uploading functionality in your application. This was a real headache for the developers who have to purchase third party components or build own component to upload files in the previous version of ASP. Also you need to know the time when the uploading completes. This is all possible with the File Field control, which is very easy to use. In this article you can see how to implement this controls functionality with example.
You can find the control in the HTML section of the toolbox. Let us try this,
Open a new Asp web application project, drop a File Field control from the HTML Control box, and Drop a label control and a Button from the Web Forms tab in the toolbox. Set the ID property of the File Field to MyFile and the label control name as lbl, also name the Button control as btnSubmit. Right click on the File Field control and select the Run as Server control property.
Now include the code block in the click event of the submit button
‘This line will check whether a file is selected or not If IsNothing(MyFile.PostedFile) Then lbl.Text = "Select one file _ to upload" : Exit Sub
‘This line will check whether the file selected is a zero length file If MyFile.PostedFile.ContentLength = 0 Then lbl.Text = "Cannot _ upload zero length file" : Exit Sub
‘Display the file name selected ‘You can use this property with regular expression to check the file type lbl.Text = MyFile.PostedFile.FileName
‘Save the file to the directory MyFile.PostedFile.SaveAs("c:\Upload\MyFile.txt")
If you are not able to upload the file check whether the directory in which you are uploading has the ASP.NET Machine Account included in the list and has the proper permissions to write to disk.
The default maximum size of a file that can be uploaded is 4mb. You can change the default behavior by editing the machine.config file (found in C:\WINNT\Microsoft.NET\Framework\v1.1.4322\CONFIG) or in the web.config file. In this file search for a tag . A lot is going on in this single node, but the setting that takes care of the size of the files to be uploaded is the maxRequestLength attribute. You can set it to the desired value in kilobytes. If you want this to affect all the applications in your server, apply it in the machine.config file else if you want to apply it only for the current application , make the change in the web.config file. Make sure this node resides between the nodes in the configuration file.
Another setting involved in the size limitation of files to be uploaded is the value given to the executionTimeout attribute in the node. The value given the executionTimeout attribute is the number of seconds the upload is allowed to occur before being shut down by ASP.NET. If you are going to allow large files to be uploaded to the server, you are also going to want to increase this value along with the maxRequestLength value.
|
| Author: Parameshwara 20 May 2004 | Member Level: Bronze Points : 0 |
Hi,
If the file size is too huge(say 500MB), what I need to do in this case ?
With Regds Paramesh
|
| Author: RAJASEKARAN 12 Jul 2004 | Member Level: Bronze Points : 0 |
THIS IS WHAT EXACTLY I WAS LOOKING FOR. F A N T A S T I C
But when I tried, it always says "Select one file" even though i had selected one. the isnothing is true. What could be reason
|
| Author: Natarajan.M 15 Jul 2004 | Member Level: Gold Points : 0 |
Simple but really good to the beginers.when i am trying to upload the file using java script it's not a secured way of upload.I think Your sample is the best method for uploding the file.
thnx a lot.
Natarajan.M Chennai
|
| Author: AJMM Sudeera Hasanta 11 Aug 2004 | Member Level: Bronze Points : 0 |
Where's the coding
|
| Author: milind 27 Sep 2004 | Member Level: Bronze Points : 0 |
Thanks for such simple and good example
|