0

So, essentially I'm trying to send a file through a post request. The file data is stored as a string.

            var fd = new FormData();
            fd.append('fileString', file.attachmentString);
            fd.append('fileName', file.fileName);

            return $.ajax({
                url: baseUrl + url,
                data: fd,
                cache: false,
                processData: false,
                contentType: false,
                type: 'POST'
            });

This works fine up to 21MB, then fails at 22MB+

The request will never reach the controller for the 22MB+ request, instead returning the error "error" with status 0.

    [HttpPost("SendFile")]
    public async Task<int> AddFile(MyFileData fileData)

That is the post request, and below is the MyFileData

    public class MyFileData
{
    public string FileString { get; set; }
    public string FileName { get; set; }
}

This all works perfectly up to 22MB, then fails to reach the request. Any ideas?

user1539405
  • 111
  • 1
  • 14

1 Answers1

2

You can increase file length limit value .

in .net core :

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .UseKestrel(options =>
    {
        options.Limits.MaxRequestBodySize = 52428800; //50MB
    });
}

and in asp.net , add these codes to web.config

<system.web>
      <httpRuntime maxRequestLength="600000"/>
</system.web>
  • I was really hoping this would be it. I'd been using maxRequestLength in .net core. Swapped it over and unfortunately the same result. Upvoted for correcting my mistake on where to set the max length though! – user1539405 Jun 07 '20 at 17:50
  • If you are hosting in IIS you need to be careful how/where you set it. In fact in dotnet core if you set it incorrectly (both places or only one place, I forget now) the very first or second log message written out to the applications console will warn you of the fact you have set it incorrectly and advises how to correct it – pinkfloydx33 Jun 07 '20 at 19:23
  • I just looked at my own app. I had to be sure to remove security.requestFiltering.requestLimits.maxAllowedContentLength from my web.config file. Then in the application, I had to use the solution from above. In my case I had set it to null because I was removing the limit entirely (for other reasons) I had struggled with this for nearly two days straight until I just happened to notice the warning message at the beginning of the application logs – pinkfloydx33 Jun 07 '20 at 19:33
  • I checked and I'm getting "blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." So I'm being blocked by the CORS policy, but it's allowing all the <22MB sends to occur – user1539405 Jun 08 '20 at 08:35
  • Check last answer of this question ( https://stackoverflow.com/questions/60617821/webapi-core-unable-to-upload-large-file-blocked-by-cors-policy ) – Arsalan Valoojerdi Jun 08 '20 at 08:58