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)     { ...

Puzzles for kids - 12 Days Of Christmas

According to the traditional song, on the first day of Christmas (25th December), my true love sent to me: . A partridge in a pair tree On the second day of Christmas (26th December), my true love sent to me THREE presents: . Two turtle doves . A partridge in a pear tree On the third day of Christmas (27th December and so on) my true love sent to me SIX presents: . Three French hens . Two turtle doves . A partridge in a pear tree This carries on until the the twelfth day of Christmas, when my true love sends me: Twelve drummers drumming Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying Five gold rings Four calling birds Three French hens Two turtle doves A partridge in a pear tree After the twelve days of Christmas are over, how many presents has my true love sent me altogether? Our Solution: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55 + 66 + 78 = 364 presents Which is really interesting when you think ...

Asp.Net DataTable Manipulation - Add, Update,Delete & Sort DataTable in C#.Net & VB.Net

In the asp.net developer's life it is very common to come across the datatable manipulation. So here i have decided to explain the datatable manipulation like adding records to datatable, editing and updating datatable records,deleting records from datatable based on the condition and finally sorting datatable based on columns.