thesecretmaster thesecretmaster - 4 months ago 20
HTML Question

Submitting a file to a sinatra backend

I have a form to upload a file:

<form action="/upload" method="post">
<input type="file" name="image">
<input type="submit">
</form>


and I am trying to see what is being submitted. In google chromes inspect element and when I use
params.inspect
in my back end the only form data that is submitted is
{:image => "<submitted file name>"}
. How can I get the actual image data? As per this website I should just recieve something in the format of:

{
"image" => {
:type => "image/png",
:head => "Content-Disposition: form-data;
name=\"myfile\";
filename=\"cat.png\"\r\n
Content-Type: image/png\r\n",
:name => "myfile",
:tempfile => #<File:/var/folders/3n/3asd/-Tmp-/RackMultipart201-1476-nfw2-0>,
:filename=>"cat.png"
}
}


I have no idea why that isn't happening. If someone can offer an explanation and correction that would be fantastic.

Answer

In order to upload files you need to set the enctype attribute on the form element to multipart/form-data:

<form action="/upload" method="post" enctype="multipart/form-data">
Comments