Ia m trying to upload and process a file using asp.net core Webapi and postman.
Here is the code on the backend side in the controller :
using System;
using Microsoft.AspNetCore.Mvc;
using System.IO;
namespace Learn_Core_API.Controllers
{
public class HomeController : Controller
{
[Route("")]
public IActionResult Index()
{
return View();
}
[HttpPost("add_file")]
[RequestFormLimits(ValueLengthLimit = int.MaxValue, MultipartBodyLengthLimit = int.MaxValue)]
public string Upload([FromBody]Microsoft.AspNetCore.Http.IFormFile File)
{
var files = HttpContext.Request.Form.Files;
var uploadDirecotroy = "uploads/";
var uploadPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, uploadDirecotroy);
if (!System.IO.Directory.Exists(uploadPath))
Directory.CreateDirectory(uploadPath);
var fileName = Guid.NewGuid() + System.IO.Path.GetExtension(File.FileName);
var filePath = System.IO.Path.Combine(uploadPath, fileName);
return fileName;
}
}
}
And here is code used in startup.cs file to configure upload :
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddMvc();
services.AddDbContext<Learn_Core_API.Models.ReunionDbContext>(options => options.UseSqlServer(ConnectionString));
services.Configure<FormOptions>(x =>
{
x.MultipartBodyLengthLimit = 10000000000;
});
}
Following are the screen captures of postman with the request made, the http header sent and the Http response error send back from the server :
- The request header
- The request Body
And as you can notice, i get the error :
System.IO.InvalidDataException: Multipart body length limit 16384 exceeded.
Any idea ? Thanks.