将文件提交到 sinatra 后端
Submitting a file to a sinatra backend
我有一个上传文件的表格:
<form action="/upload" method="post">
<input type="file" name="image">
<input type="submit">
</form>
我正在尝试查看正在提交的内容。在 google chrome inspect element 中,当我在后端使用 params.inspect
时,提交的唯一表单数据是 {:image => "<submitted file name>"}
。我怎样才能得到实际的图像数据?根据 this website 我应该只收到以下格式的内容:
{
"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"
}
}
我不知道为什么没有发生这种情况。如果有人可以提供解释和更正,那就太好了。
为了上传文件,您需要将 form
元素上的 enctype
attribute 设置为 multipart/form-data
:
<form action="/upload" method="post" enctype="multipart/form-data">
我有一个上传文件的表格:
<form action="/upload" method="post">
<input type="file" name="image">
<input type="submit">
</form>
我正在尝试查看正在提交的内容。在 google chrome inspect element 中,当我在后端使用 params.inspect
时,提交的唯一表单数据是 {:image => "<submitted file name>"}
。我怎样才能得到实际的图像数据?根据 this website 我应该只收到以下格式的内容:
{
"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"
}
}
我不知道为什么没有发生这种情况。如果有人可以提供解释和更正,那就太好了。
为了上传文件,您需要将 form
元素上的 enctype
attribute 设置为 multipart/form-data
:
<form action="/upload" method="post" enctype="multipart/form-data">