-1

I am trying to pass multiple pdf files to the controller method. This is my code in javascript:

var formData = new FormData();
formData.append("data", JSON.stringify(data));
formData.append("httpPostedFileBase", file1, "ff2.pdf");
formData.append("httpPostedFileBase", file1, "ff1.pdf");
$.ajax({
  type: "POST",
  url: "@Url.Action("GetFiles", "FileUpload")",
  data: formData,
  dataType: 'json',
  contentType: false,
  processData: false,
  success: function () {
    console.log("5");
 },
 error: function () {
   alert('Failure to send request');
 }
});

And this is in the controller:

controller

public ActionResult GetFiles(HttpPostedFileBase[] httpPostedFileBase, string data) {
  try {
    if (Request.Files.Count > 0) {
       //...

I get a "Failure to send request" alert right away:

error message

It does not even reach the controller method.

However, works fine if I only try to append a single file. The funny thing is that I've written this functionality in a different project and it works well there.

I tried passing an array of blobs instead of appending them one by one to formData but that did not work either. Any help would be appreciated.

Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
Jane Doe
  • 1
  • 2

1 Answers1

0

Since another project of yours is working, could it be that you forgot to set an explicit max request size limit in this one? It happened on some projects that the combined file size exceeded max request limit for .NET allowance.

It might require various steps as shown here:

Try checking this

"By default, ASP.NET Core allows you to upload files up of (approximately) 28 MB in size"

In the configuration of the applications (either Startup.cs if you're on <= .NET 5 or Program.cs starting from .NET 6):

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.Configure<FormOptions>(options =>
    {
         // Set the limit to 128 MB for example
         options.MultipartBodyLengthLimit = 134217728;
    });
}

But also on IIS Side (remember when you're going to deploy):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <!-- Removed other configuration -->
    <security>
      <requestFiltering>
       <!-- 128MB = 134,217,728  Bytes -->
        <requestLimits maxAllowedContentLength="134217728"/>
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

In the article also explains how to behave for kestrel servers.

Liquid Core
  • 1
  • 6
  • 27
  • 52