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

C# Extension Methods Example

In this article i am going to explain about Extension Methods  with example. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type. If you do implement extension methods for a given type, remember the following points: An extension method will never be called if it has the same signature as a method defined in the type. Extension methods are brought into scope at the namespace level. For example, if you have multiple static classes that contain extension methods in a single namespace named Extensions, they will all be brought into scope by the using Extensions; directive. ...

CHECK IF FUNCTION EXISTS EXAMPLE

http://css-tricks.com/snippets/javascript/check-if-function-exists-before-calling/ When using scripts that are shared between different areas of a site, there may be cases where a function is called that doesn't exist. It makes sense on one page (the dependency is there) but not another. The difference is too slight to warrant forking the file into different versions. Instead, you can just check if the function exists before calling it to avoid the error: if ( typeof yourFunctionName == 'function' ) { yourFunctionName ( ) ; } jQuery code snippet to check whether a function exists within the JavaScript code. This can be easily achieved by using the jQuery.isFunction() function. Useful for checking if a jQuery function exists before calling it! < script type = "text/javascript" >      function  somenoobfunction ( )   {      }         // using jQuery      if   ( $. isFunction ( wi...