Hello, I need to create a custom massage or page for Sending large request, for example if my user sends a file larger than 2 mg, my code shows them a page or redirect or something except default massage,
I have edited this line in endpoint.ex
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
body_reader: {WeddingCardWeb.DashboardController, :body_of_upload, []},
json_decoder: Phoenix.json_library()
and my functions in my controller is:
def body_of_upload(conn, _opts) do
conn
|> put_flash(:error, "متاسفانه فایل ارسالدر مواردی مثل حجم و فرمت مجاز دارای مشکل می باشد.")
|> redirect(to: "/dashboard")
end
but it doesn’t work.
By the way, can I create a preventer to limit my user sending upload file in my function:
def upload(conn, params) do
with {:ok, user_id} <- check_session(conn, :user_login_id), {:ok, invoice_id_valid} <- Ecto.UUID.cast(params["invoice_id"]),
{:ok, user_infos} <- WeddingCardWeb.LoginController.user_find_pattern(user_id), true <- user_infos.status,
{:ok, invoice_info} <- invoice_pattern_by_id(invoice_id_valid),
{:ok, :belong_user} <- invoice_belong_user_pattern(user_id, invoice_info.wedding_user_id),
{:ok, upload} <- upload_check(params["upload"]), {:ok, [file_name]} <- file_name_reader(upload),
true <- validate_file(file_name),
nil <- WeddingCard.DB.UploadQuery.get_upload_by_invoice_id(invoice_info.id) do
[%{file_size: size, file_name: name_of_file}] = config_file(upload)
# Store in db
WeddingCard.DB.UploadQuery.insert_upload(%{
"wedding_user_id" => user_infos.id,
"wedding_invoice_id" => invoice_info.id,
"size" => "#{size}",
"address" => "#{name_of_file}"
})
conn
|> put_flash(:info, "فایل شما با موفقیت آپلود گردید")
|> redirect(to: "/dashboard")
else
_ ->
conn
|> put_flash(:error, "متاسفانه فایل مذکور ارسال نگردید یا در فرمت و حجم مناسب نبوده است. توجه فرمایید شما فقط مجاز به ارسال فایل برای یک بار هستید.")
|> redirect(to: "/dashboard")
end
end
all the functions I called top in my function, they work after uploaded file!!!
for example:
def validate_file(filename) do
~w(.jpg .jpeg .xlsx .png .JPG .pdf .docx) |> Enum.member?(Path.extname(filename))
end
it works after uploading, but the file concerned is bigger than my limitation, and my project shows me a default massage like this that I don’t want it.
Request Entity Too Large
Thanks