Get the size of the uploaded file

Let's get the size of the uploaded file. To get the size of the uploaded file, use the size method of the Mojo::Upload class. To do.

my $size = $upload->size;

When uploading a file from a form, set the "enctype" attribute of the "form" element to "multipart / form-data" on the form and use the file field (file_field) to param method of the .mojodoc.perlzemi.com/Mojolicious::Controller.html "> Mojo::Controller class You can get the file as a Mojo::Upload object.

HTML side

<form method = "POST" enctype = "multipart / form-data">
  <%= file_field'image_file'%>
</form>

Program side

# Uploaded file
my $image_file = $c->param('image_file');

Check if the file has been uploaded

One thing to note is that uploading from a form will generate a Mojo::Upload object even if the file wasn't uploaded. Therefore, it is not possible to check whether the file has been uploaded with the defined function. You need to check the file size.

#Check for correct upload
if ($image_file->size> 0) {
  
}

# Check for wrong uploads
if (defined $image_file) {
  
}

Associated Information