0
votes

I have developed an ASP.NET core web api to upload files from one place to other place. I test API by using postman. By default ASP.NET core accepts upto 28 MB while uploading file. For IIS express approach,I increased size of maxAllowedContentLength in wev.config file and for Kestrel approach, I increased size of MaxRequestBodySize. But all these approaches work fine upto file size of 200 MB but uploading file size more than 200 MB is getting failed and even [DisableRequestSizeLimit] is getting failed . I set values of maxAllowedContentLength and MaxRequestBodySize more than 1 gb. Please suggest me an approach to upload large file more than 1 gb in ASP.NET Core web api. Any help is appreciated.

IIS Express:

 <requestFiltering>
        <!-- Measured in Bytes -->
        <requestLimits maxAllowedContentLength="1073741824"  />
        <!-- 1 GB-->
      </requestFiltering>

Kestrel:

 public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.ConfigureKestrel((context, options) =>
                    {
                        options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(120);

                        options.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(120);
                        options.Limits.MaxRequestBodySize = 5242880000;
                       
                    webBuilder.UseStartup<Startup>();
                });

 [DisableRequestSizeLimit]
        [HttpPost]
        public  IActionResult PostUploadFiles([FromForm] List<IFormFile> postedFiles)
        {
            try
            {
                string wwwPath = this.Environment.WebRootPath;
                string contentPath = this.Environment.ContentRootPath;


                string path = Path.Combine(this.Environment.ContentRootPath, "Uploads");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                List<string> uploadedFiles = new List<string>();
                foreach (IFormFile postedFile in postedFiles)
                {
                    string fileName = Path.GetFileName(postedFile.FileName);
                    using (FileStream stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
                    {

                        postedFile.CopyTo(stream);
                        FileInfo file = new FileInfo(Path.Combine(path, fileName));
                        uploadedFiles.Add(fileName);
                      

                    }
                }

                Artifactory ar = new Artifactory();
                ar.FileName = "Sample";
                ar.Id = 1;
                ar.FileSize = 1024;
                string jsonConverted = JsonConvert.SerializeObject(ar);

                return new ObjectResult("File has  been uploaded") { StatusCode = Convert.ToInt32(HttpStatusCode.Created) };
            }
            catch (Exception ex)
            {
                return new ObjectResult(postedFiles) { StatusCode = Convert.ToInt32(HttpStatusCode.BadRequest) };
            }



        }
1
try adding: services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = <value>});Andy
@Andy-I added your suggestion and it works fine.services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = <value>}); I uploaded a file with size of 724 MB and it worked. Thanks for your help.Adithyaa Veerapan
@Andy-Could you please suggestion a solution to fix this issue in IIS Express?Adithyaa Veerapan
@Andy- I tried the solution mentioned for IIS on stackoverflow.com/questions/53829487/…. but it did not work.Adithyaa Veerapan

1 Answers

0
votes

For IIS,except setting maxAllowedContentLength in Request Filtering. You also need to set like this: If you want to remove the file upload limit,you can set it to null.

services.Configure<IISServerOptions>(options =>
            {
                options.MaxRequestBodySize = xxx;
            });

And If you run your server in IIS, in addition to setting the MaxRequestBodySize property, you must also remove the Request Filtering module.