thesecretmaster thesecretmaster - 1 year ago 69
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">

and I am trying to see what is being submitted. In google chromes inspect element and when I use
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;
Content-Type: image/png\r\n",
:name => "myfile",
:tempfile => #<File:/var/folders/3n/3asd/-Tmp-/RackMultipart201-1476-nfw2-0>,

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

Answer Source

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">