Skip to main content

Upload multiple files in asp.net C#.Net VB.Net / Maximum Request Length Exceeded

In this tutorial i am going to explain about how to upload multiple files in asp.net, c#.net and vb.Net with out using any library.

With the introduction of html 5 it is possiblt to upload multiple files at once by setting AllowMultiple attribute of FileUpload control in asp.net. But the drawback in this solution is it will work only in ASP.Net 4.5+ Frameworks and browsers that support html5 i.e. IE 10+, FireFox and Chrome.

For the explanation purpose i have included a FileUpload control with AllowMultiple property set to true. And i have a button control btnUploadFiles with click event handled by the event handler btnUploadFiles_Click. And i have a literal called ltrMessage to show the error/status messages. At first the files will be selected and then while clicking on the Upload File button it will start uploading to the specified folder mentioned in the code (here it is file folder) and then it will show the no of uploaded files to server in the literal.

Below is the html markup used for the demo purpose.
HTML Code:

Include the below namespace in your code behind file
C#.Net
using System.IO;
Vb.Net
Imports System.IO

Below code is used to upload files.
C#.Net
protected void btnUploadFiles_Click(object sender, EventArgs e)
{
int FilesCount = 0;
foreach (HttpPostedFile postedFile in fuFiles.PostedFiles)
{
    if (postedFile.ContentLength > 0)
    {
        FilesCount++;
        string fileName = Path.GetFileName(postedFile.FileName);
        postedFile.SaveAs(Server.MapPath("~/Files/") + fileName);
    }
}
ltrMessage.Text = string.Format("{0} files have been uploaded successfully.",
    FilesCount.ToString());
}
VB.Net
Protected Sub btnUploadFiles_Click(sender As Object, e As EventArgs)
 Dim FilesCount As Integer = 0
 For Each postedFile As HttpPostedFile In fuFiles.PostedFiles
  If postedFile.ContentLength > 0 Then
   FilesCount += 1
   Dim fileName As String = Path.GetFileName(postedFile.FileName)
   postedFile.SaveAs(Server.MapPath("~/Files/") & fileName)
  End If
 Next
 ltrMessage.Text = String.Format("{0} files have been uploaded successfully.", FilesCount.ToString())
End Sub

Below is the output:
Upload multiple files in asp.net C#.Net VB.Net / Maximum Request Length Exceeded

In asp.net the default upload size is 4MB. So if the file size exceeds 4 MB then you will encounter the error -Maximum Request Length Exceeded
Maximum Request Length Exceeded

To overcome this error you suppose to set the maximum file size in web.config as shown in figure.


    



For IIS and above you need to add the below code also.


    
        
    



 Upload Multiple Files In Asp.Net C#.Net VB.Net / Maximum Request Length Exceeded

You may also like:
  1. ASP.Net - Validate form using css in C#.Net - 
  2. ASP.Net - Bind Array To DropDownList in C#.Net,VB.Net
  3. Check textbox is changed or not using javascript C#.net
  4. Check uncheck all checkboxes in grid view using jquery
  5. Code To Convert rupees(numbers) into words using C#.Net
  6. Code to Convert Dataset Datatable to Json array in C#.Net/Asp.Net
  7. Code to clear all text boxes in C#.Net/ASP.Net
  8. Code to create log files in C#.Net|Asp.Net
  9. Delete browser cookie using C#.Net,Asp.Net
  10. Dynamically programmatically add contols at run time Asp.Net
  11. Encrypt Decrypt password string in C#.Net Asp.Net

Comments

Popular posts from this blog

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     { ...

Various components of the .NET Platform and the functions performed by them

Now we will go in detail about the various components that build the .NET framework and its functionalities. Common Language Runtime At the core of the .NET platform is the Common Language Runtime (CLR). The CLR simplifies application development, provides a robust and secure execution environment, supports multiple languages and simplifies application deployment and management. The diagram below provides more details on the CLR's features: In this section we will cover some of the more significant features provided to .NET applications by the CLR. These include: • Memory Management • Common Type System Before moving further let us discuss briefly about Common Language Infrastructure(CLI) according to Standardizing Information and Communication Systems(ECMA) specifications. The Microsoft Shared Source CLI Implementation is a file archive containing working source code for the ECMA-334 (C#) and ECMA-335 (Common Language Infrastructure, or CLI) s...