How to Upload files in ASP .NET?

Syedshakeer
Posted by Syedshakeer under ASP.NET category on | Views : 4023
How to Upload files in ASP .NET?

Asking the user to upload a file to the server is a very very common task in most of the web applications. In classic ASP, uploading file was a very difficult task, since we need a third party component or we need to write our component to upload file from client to the server. In ASP .NET Uploading a file from client to the web server is so easy. To be precise, we can get this done in two lines of code. Yes, two lines of code. In this article, we will be looking into the following:

1. How to Upload a file from Client to the Web server in ASP .NET?
2. How to give a different name to the file saved to the web server?
3. How to know the size of the file uploaded by the user?
4. How to control the size of file uploaded to the web server?

How to Upload a file from Client to the Web server in ASP .NET?
First of all, we need a HTML server control to allow the user to select the file. This is nothing but the same old <input tag, with the type set to File, such as <input type=file id=myFile runat=server />. This will give you the textbox and a browse button. Once you have this, the user can select any file from their computer (or even from a network). Then, in the Server side, we need the following line to save the file to the Web Server.

myFile.PostedFile.SaveAs("DestinationPath")

Example: Uploading a File in ASP .NET
<html>
<head>
<script language="VB" runat="server">

Sub Upload(Source As Object, e As EventArgs)

If Not (myFile.PostedFile Is Nothing) Then

Dim intFileNameLength as Integer
Dim strFileNamePath as String
Dim strFileNameOnly as String

'Logic to find the FileName (excluding the path)
strFileNamePath = MyFile.PostedFile.FileName
intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)

myFile.PostedFile.SaveAs("c:\inetpub\wwwroot\yourwebapp\upload\" & strFileNameOnly)
lblMsg.Text = "File Upload Success."

End If
End Sub
</script>

</head>
<body>

<h3>File Upload</h3>

<form enctype="multipart/form-data" runat="server">
File: <input id="myFile" type="file" runat="server">
<asp:label id=lblMsg runat="server" />
<input type=button value="Upload" OnServerClick="Upload" runat="server">
</form>

</body>
</html>


Test this Script
In the above example, you should note that, in the FORM tag, we have set the ectype to "multipart/form-data". This is another important aspect while uploading a file to the web server. We have a Input tag which is of type file. Then we have a button called "Upload". On the onclick of the button, you can see that, we are using some string functions to find the FileName (excluding the path). Finally, we invoke the method SaveAs to save the file (upload) in the web server.
How to retrieve the properties of file uploaded by the user?

We can retrieve some of the interesting and useful properties of the file that is uploaded to the webserver. The PostedFile provides us with three important properties, such as FileName, ContentType and ContentLength. In our previous example, we already saw, how to use the property, FileName? Now, we will see, how to know the size of the file uploaded, and how to know the contentType of the file uploaded.

The following line of code retrieves the ContentType of the file uploaded.

lblFileContentType.Text = MyFile.PostedFile.ContentType


The following line of code retrieves the Size of the file uploaded.

lblFileSize.Text = CStr(MyFile.PostedFile.ContentLength)


Now, we will see an example which uses this property.

Example: Uploading a File in ASP .NET

<html>
<head>
<script language="VB" runat="server">

Sub Upload(Source As Object, e As EventArgs)

If Not (myFile.PostedFile Is Nothing) Then

Dim intFileNameLength as Integer
Dim strFileNamePath as String
Dim strFileNameOnly as String

'Logic to find the FileName (excluding the path)
strFileNamePath = MyFile.PostedFile.FileName
intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)

myFile.PostedFile.SaveAs("c:\inetpub\wwwroot\yourwebapp\upload\" & strFileNameOnly)
lblMsg.Text = "File Upload Success."
lblFileContentType.Text = "Content type: " & MyFile.PostedFile.ContentType
lblFileSize.Text = "File size: " & CStr(MyFile.PostedFile.ContentLength) & " bytes"

End If
End Sub
</script>

</head>
<body>

<h3>File Upload</h3>

<form enctype="multipart/form-data" runat="server">
File: <input id="myFile" type="file" runat="server"> <input type=button value="Upload" OnServerClick="Upload" runat="server">
<asp:label id=lblMsg runat="server" />
<asp:label id=lblFileContentType runat="server" />
<asp:label id=lblFileSize runat="server" />
</form>

</body>
</html>


Test this Script
How to control the size of file uploaded to the web server?
While uploading a file to the web server, we have a limit of 4MB by default. We can either decrease or increase this value. The value is set in the key, maxRequestLength of machine config file.

There is a maxRequestLength limit in the machine.config file (look for the <system.web> section), in the httpRuntime settings that you need to alter/raise if you want to accept anything larger than 4Mb. These are the standard settings for the httpRuntime:

<httpRuntime executionTimeout="90" maxRequestLength="4096"
useFullyQualifiedRedirectUrl="false" minFreeThreads="8"
minLocalRequestFreeThreads="4" appRequestQueueLimit="100"/>

Comments or Responses

Login to post response