Handle two different size limit using Plug.Parsesr

I’ve two diferent file format in my project one is image type another one is csv type. For the image type, i want to set the 500 kB as limit and for CSV i want to set 8 mb as limit.

I’ve already set image size limit by using Plug.Parser in the endpoint. So, it’s global now.

plug Plug.Parsers,
    parsers: [:urlencoded, {:multipart, length: 500_000}, :json],
    body_reader: {ApplicationWeb.Plugs.CacheBodyReader, :read_body, []},
    pass: ["*/*"],
    json_decoder: Phoenix.json_library()

Now, How can i handle this two file size limit ?

That limit is meant as a limit to prevent bad actors from being able to flood your server with big requests. It’s not meant as validation of filesizes of different files. Set it to whatever the biggest file you want to accept and do the rest elsewhere as part of your validation.

1 Like