How to upload large file to server using FileUpload control


FileUpload control in ASP.NET is used to upload the file to the server. If we want to upload the large file which is having more than 4096 KB, then we have to change the HttpRunTime Parameter maxRequestLength.

FileUpload control in ASP.NET is used to upload the file to the server.
The following code shows how to use the FileUpload control:



<%@ Page Language="C#" %>

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

<script runat="server">
protected void btnFileUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{

FileUpload1.SaveAs(@"D:\temp\" + FileUpload1.FileName);
lblMessage.Text = "File Uploaded: " + FileUpload1.FileName;
}
else
{
lblMessage.Text = "No File Uploaded.";
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Upload a file to server</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="btnFileUpload" runat="server" OnClick="btnFileUpload_Click" Text="Upload File" Width="105px" /><br />

<asp:Label ID="lblMessage" runat="Server"></asp:Label>

</div>
</form>
</body>
</html>




By default, the maximum file size that can be uploaded to the server is 4096KB. To upload larger files, you need to make change either in the Machine.config or Web.config for httpRuntime element. The maxRequestLength specifies the limit for the file to be uploaded, in KB.

The following code shows how to specify HTTP run-time parameters for an ASP.NET application.



<configuration>
<system.web>
<httpRuntime maxRequestLength="4096"
enable = "True"
requestLengthDiskThreshold="512
useFullyQualifiedRedirectUrl="True"
executionTimeout="45"
versionHeader="1.1.4128"/>
</system.web>
</configuration>


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: