【问题标题】:Submitting a file to a sinatra backend将文件提交到 sinatra 后端
【发布时间】:2016-11-15 08:38:01
【问题描述】:

我有一个表格可以上传文件:

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

我正在尝试查看正在提交的内容。在谷歌浏览器检查元素中,当我在后端使用params.inspect 时,提交的唯一表单数据是{:image =&gt; "&lt;submitted file name&gt;"}。如何获取实际的图像数据?根据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"
  }
}  

我不知道为什么没有发生这种情况。如果有人可以提供解释和更正,那就太好了。

【问题讨论】:

    标签: html forms file input sinatra


    【解决方案1】:

    为了上传文件,您需要将form 元素上的enctype attribute 设置为multipart/form-data

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-01
      • 2020-06-01
      • 1970-01-01
      • 2020-11-02
      • 2015-09-05
      • 2020-06-25
      • 2021-03-12
      • 1970-01-01
      相关资源
      最近更新 更多