Wednesday, February 8, 2012

Upload large file in ASP.Net


Upload a large file in ASP.Net

To upload a large file using ASP.Net 2.0 you have to configure web.xml file. Add the following code in your web.config file.
<httpRuntime
 executionTimeout="110"
 maxRequestLength="500000"
 requestLengthDiskThreshold="80"
 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" />


The parameter maxRequestLength control maximum size of file allow to upload. Change the parameter value according to your requirement. Now you can allow to upload your won favorite file.
Add the content in a ASP.Net page.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" /></div>
    </form>
</body>
</html>

Now add the content in button’s click event.

protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            try
            {
                FileUpload1.SaveAs("D:\\" + FileUpload1.FileName);
                Response.Write("Filename-"+FileUpload1.FileName+"<br/>");
                Response.Write("File size-" + FileUpload1.PostedFile.ContentLength + "<br/>");
                Response.Write("File Type-" + FileUpload1.PostedFile.ContentType + "<br/>");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

    }

No comments:

Post a Comment