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
using System.IO;
Imports System.IO

Below code is used to upload files.
protected void btnUploadFiles_Click(object sender, EventArgs e)
int FilesCount = 0;
foreach (HttpPostedFile postedFile in fuFiles.PostedFiles)
    if (postedFile.ContentLength > 0)
        string fileName = Path.GetFileName(postedFile.FileName);
        postedFile.SaveAs(Server.MapPath("~/Files/") + fileName);
ltrMessage.Text = string.Format("{0} files have been uploaded successfully.",
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
 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


Post a Comment

Online Casino