eprint_4_10581_2

advertisement

File Upload WebServer Control

Written By Hassan H. Alrehamy

ASP.NET introduces built-in way of designing IT applications that have the ability to upload files to the Production server. This can be done through the use of FileUpload.

FileUpload control is an ASP.Net Server Side Control that allows users to browse [from there browsers] for files and then posting them to the Web Server . The FileUpload Class in ASP.Net provides many properties and methods that help developers to build clean,secure uploading

interface .

The most important properties and methods of this control are :

1.

HasFile : a boolean property that returns (get) value indecates whether the user has selected a file or not , it returns only true/false

2.

FileBytes : a property returns an array of bytes represents the uploaded file

3.

FileName : a property returns string represents the name of uploaded file

4.

postedFile : a property returns the Underlying object of the file that has been uploaded using FileUpload Control , its very important property because the returned object contains a set of methods and properties that are very handy while developing Upload applications :

FileName : a property returns string repsents the fully qualified name of the Uploaded file

ContentType : a property returns the MIME Type of the File sent by the client

ContentLength : a property returns integer value indecates the full size of the file sent by the Client in bytes

SaveAs : save the contents of the file on the physical Qualified path (on the Production Server )

5. SaveAs : a void method used to save files sent by client and transmitted over the internet on the server . this is the main method of FileUpload Control . it accepts the physical path in string type only .

How Does It Work

?

By using the FileUpload control that converts itself to an <input type="file"> tag, the browser automatically places a Browse button next to the text field on the ASP.NET page. You don't have to program anything else for this to occur. When the end user clicks the Browse button, he can navigate through the local file system to find the file to be uploaded to the server. This is shown in Figure 3. Clicking Open will place that filename and the file's path within the text field .

Size Problem in FileUpload Control

the FileUpload control Max Limit is around 4MB. You cannot upload anything that is larger than this limit. You can usually change the default settings. To change this size limit, you make some changes in your application's web.config file .

<httpRuntime executionTimeout="110" maxRequestLength="4096 " />

If you want to allow 10 megabyte (MB) files to be uploaded to the server, set the maxRequestLength value to 11264, meaning that the application allows files that are up to

11000 KB to be uploaded to the server.. Make sure this node resides between the

<system.web> 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

<httpRuntime> 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 .

examples of FileUpload Control

: protected void Button1_Click(object sender, EventArgs e)

{

if (FileUpload1.HasFile)

{

string fileExt =

System.IO.Path.GetExtension(FileUpload1.FileName);

if (fileExt == ".mp3")

{

try

{

FileUpload1.SaveAs("C:\\Uploads\\" +

FileUpload1.FileName);

Label1.Text = "File name: " +

FileUpload1.PostedFile.FileName + "<br>" +

FileUpload1.PostedFile.ContentLength + " kb<br>" +

"Content type: " +

FileUpload1.PostedFile.ContentType;

}

catch (Exception ex)

{

Label1.Text = "ERROR: " + ex.Message.ToString();

}

}

else

{

Label1.Text = "Only .mp3 files allowed!";

}

}

else

{

Label1.Text = "You have not specified a file.";

}

}

Download