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) };
}
}
services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = <value>});
– Andy