Monday, January 4, 2010

Uploading Multiple Files in ASP.NET 2.0

In ASP.NET We can upload more than one file using the following classes:

HttpFileCollection
HttpPostedFile
Request.Files
System.IO.Path

HttpFileCollection:

HttpFileCollection will provide access to the files uploaded by a client.

HttpPostedFile:

HttpPostedFile will provide access to individual files uploaded by a client. Through this class we can access the content and properties of each individual file, and read and save the files.

Request.Files :

The Request.Files will return collection of all files uploaded by user and store them inside the HttpFileCollection.

Follow these steps mentioned below to do so:

Step 1:

Drag and drop multiple (in our case four) FileUpload controls on to the designer.

Step 2:

Drop a Button control and rename it to “Upload”
<asp:Button ID=”btnUpload” runat=”server” Text=”Upload” />

Step 3:

Double click the Upload Button to add an event hander to the code behind.
protected void btnUpload_Click(object sender, EventArgs e)
{

}

Step 4: Import the System.IO namespace.
using System.IO;

Step 5:

Use the ‘HttpFileCollection’ class to retrieve all the files that are uploaded. Files are encoded and transmitted in the content body using multipart MIME format with an HTTP Content-Type header. ASP.NET extracts
this information from the content body into individual members of an HttpFileCollection.

The code would look as follows:
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
// Get the HttpFileCollection
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
hpf.SaveAs(Server.MapPath(”MyFiles”) + “\\” +
Path.GetFileName(hpf.FileName));

}
}
}
catch (Exception ex)
{
// Handle your exception here
}

}
Some important points to consider while uploading

1.    To save a file to the server, the account associated with ASP.NET must have sufficient permissions on the folder, where the files are being uploaded. This would usually be the ‘ASPNET’ account for Windows XP or a similar OS. In Windows Server 2003, the account used is ‘NETWORKSERVICE’. So you would be required to explicitly grant write permissions to these accounts on the folder.

2.    While uploading the files to a remote server, the default ASPNET user account used by ASP.NET does not have network permissions by default. The solution is to either give the account such permissions or
use impersonation to have it run under a different account that has the permissions.

3.    By default, you can upload no more than 4096 KB (4 MB) of data. However there is a workaround for this limitation. You can change the maximum file size by changing the maxRequestLength attribute of the
httpRuntime element in the web.config file. You can also increase the ‘executionTimeout’. By default it is 110 seconds. I would encourage you to experiment with the other attributes of the httpRuntime element.

<configuration>
<system.web>
<httpRuntime
executionTimeout=”200″
maxRequestLength=”8192″
requestLengthDiskThreshold=”256″
useFullyQualifiedRedirectUrl=”false”
minFreeThreads=”8″
minLocalRequestFreeThreads=”4″
appRequestQueueLimit=”5000″
enableKernelOutputCache=”true”
enableVersionHeader=”true”
requireRootedSaveAsPath=”true”
enable=”true”
shutdownTimeout=”90″
delayNotificationTimeout=”5″
waitChangeNotification=”0″
maxWaitChangeNotification=”0″
enableHeaderChecking=”true”
sendCacheControlHeader=”true”
apartmentThreading=”false”/>
</system.web>
</configuration>

References

There are a number of good resources I referred to, for this article. A few of them are:

http://www.wrox.com/WileyCDA/Section/id-292160.html
http://msdn2.microsoft.com/en-US/library/aa479405.aspx
http://msdn2.microsoft.com/en-us/library/system.web.httpfilecollection.aspx

No comments:

Post a Comment

Ideal SQL Query For Handling Error & Transcation in MS SQL

BEGIN TRY BEGIN TRAN --put queries here COMMIT; END TRY BEGIN CATCH IF @@TRANCOUNT>0 BEGIN SELECT @@ERROR,ERRO...