0

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

enter image description here

  • The request Body

enter image description here

And as you can notice, i get the error :

System.IO.InvalidDataException: Multipart body length limit 16384 exceeded.

Any idea ? Thanks.

the smart life
  • 305
  • 8
  • 26
  • You can try the solutions in this post first, which may help you:https://stackoverflow.com/questions/55582335/invaliddataexception-multipart-body-length-limit-16384-exceeded – Tupac Nov 29 '21 at 02:53

1 Answers1

0

Try it:

[HttpPost("add_file")]
[DisableRequestSizeLimit] //<======= add this line
[RequestFormLimits(ValueLengthLimit = int.MaxValue, MultipartBodyLengthLimit = int.MaxValue)]

public string Upload([FromForm]IFormFile file) //<=== change it to 'FromForm'
{
         if (file == null || file.Length < 1)
                return BadRequest("file is empty");
         await using (Stream fileStream = new FileStream("Your path ...", FileMode.Create))
          {
                await file.CopyToAsync(fileStream);
          }
        return Ok();
}

iis web.config:

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="2147483648" />
    </requestFiltering>
  </security>
</system.webServer>

nginx:

http {
    ...
    client_max_body_size 2G;
}
foad abdollahi
  • 1,733
  • 14
  • 32