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)     {         sb.Append( "Minus " );         inputNo = -inputNo;     }     string [] words0 = { "" , "One " ,

C# code to send mail using smtp from gmail,yahoo mail and live mail

Introduction: In my previous article I have explained about   How to bind/Unbind events in jQuery . In this article I am going to explain about how to send mail from ASP.Net using gmail,yahoomail and live mail credentials. Explanation: First Include the below namespaces in your code behind file. using System; using System.Net; using System.Net.Mail;

Geckofx Browser in Winform Application

Bored with IE browser in your winform application ? Want to do everything as you doing in your Firefox or Chrome Browser ? Play with automation ? Then here is your choice . Introduction:  GeckoFX is a Windows Forms control written in clean, commented C# that embeds the Mozilla Gecko browser control in any Windows Forms Application. It also contains a simple class model providing access to the HTML and CSS DOM . GeckoFX was originally created by Andrew Young for the fast-growing visual CSS editor, Stylizer . It is now released as open-source under the Mozilla Public License.  You can download it here :   Geckofx 22.0 And the supporting file Xulrunner here :   Xulrunner Files Hope you have downloaded above two files. Here our journey going to start. Create your winform application in visual studio and do the following: right click the toolbox -> Choose items -> Browse the "Geckofx-winforms.dll" and click "yes" for “Load it anyw