param被标识为字符串而不是文件

时间:2022-09-10 20:29:45

In my view file submit.html.erb, I decided to use the <form> tag straightforwardly, instead of the form helpers included with rails. Here is the view file:

在我的视图文件submit.html.erb中,我决定直接使用

标记,而不是rails中包含的表单助手。这是视图文件:

<form action = "/winter" method="post">
<input type="file" name="doc">
<p> Upload your question. </p>
<input type = "submit">
 <input name="authenticity_token" type="hidden" 
      value="....">
</form>

Submitting this form invokes a post request on /winters with parameters

提交此表单会在/ winters上调用带有参数的帖子请求

{"doc"=>"1.docx", "authenticity_token"=>"...."}

While I expected the doc parameter to be the file 1.docx I uploaded, the doc parameter continues to be the string original_filename of file 1.docx.

虽然我希望doc参数是我上传的文件1.docx,但doc参数仍然是文件1.docx的字符串original_filename。

My routes.rb file has the code

我的routes.rb文件有代码

post "/winter" => "paper#submit"

while inside the submit method of paper_controller is the code:

而在paper_controller的submit方法里面是代码:

File.write("Papers/rain.docx", params[:doc].read)
redirect_to "/paper"

And correspondingly, when I submit the aforementioned form, I go the /winters url and get the error

相应地,当我提交上述表格时,我会去/冬天的网址并得到错误

undefined method 'read' for "1.docx":String

undefined方法'read'代表“1.docx”:String

So why is the doc parameter set as the filename rather than the file itself? This is in contrary to the docs, I suppose: [http://guides.rubyonrails.org/form_helpers.html#what-gets-uploaded]

那么为什么doc参数设置为文件名而不是文件本身?我认为这与文档相反:[http://guides.rubyonrails.org/form_helpers.html#what-gets-uploaded]

One more point, probably relevant here, is that, there in the doc's first paragraph it says:

还有一点,可能与此相关的是,在文档的第一段中,它说:

Depending on the size of the uploaded file it may in fact be a StringIO or an instance of File backed by a temporary file.

根据上传文件的大小,它实际上可能是StringIO或由临时文件支持的File实例。

So, is it saying that sometimes the params[:doc] might be a file instance, other times it might be a string? How to deal with such random behavior?

那么,它是否说有时params [:doc]可能是一个文件实例,有时它可能是一个字符串?如何处理这种随机行为?

And, one more thing, what if I tried get request instead of put request? get has to put the params right after the url, as a query string, right? So should then params[:doc] be always a string? I tried using get, and was sent to the url

而且,还有一件事,如果我尝试获取请求而不是放置请求怎么办?得到必须把params放在url之后,作为查询字符串,对吧?那么params [:doc]应该总是一个字符串吗?我尝试使用get,并被发送到网址

http://localhost:3000/winter?doc=1.docx

(I wasn't using the authenticity token params' hidden input then). And of course, the same

(我当时没有使用真实性令牌参数的隐藏输入)。当然,同样的

undefined method 'read' for "1.docx":String

undefined方法'read'代表“1.docx”:String

error occurred.

发生了错误。

1 个解决方案

#1


1  

You have to use multipart/formdata encoding for file transfers to work. This is true for any web framework.

您必须使用multipart / formdata编码才能使文件传输正常工作。对于任何Web框架都是如此。

You should also just get over the "waah learning is hard" thing and just use the form helpers as it will add the correct authenticity token:

您还应该克服“waah learning is hard”的问题并使用表单助手,因为它会添加正确的真实性令牌:

<%= form_tag("/winter", multipart: true) do %>
  <%= file_field_tag 'doc' %>
  # ...
<% end %>

And, one more thing, what if I tried get request instead of put request? get has to put the params right after the url, as a query string, right? So should then params[:doc] be always a string? I tried using get, and was sent to the url

而且,还有一件事,如果我尝试获取请求而不是放置请求怎么办?得到必须把params放在url之后,作为查询字符串,对吧?那么params [:doc]应该总是一个字符串吗?我尝试使用get,并被发送到网址

You cannot upload files in a GET request. They must be included in the request body of a POST/PATCH/PUT request with multipart/formdata encoding.

您无法在GET请求中上传文件。它们必须包含在具有multipart / formdata编码的POST / PATCH / PUT请求的请求主体中。

#1


1  

You have to use multipart/formdata encoding for file transfers to work. This is true for any web framework.

您必须使用multipart / formdata编码才能使文件传输正常工作。对于任何Web框架都是如此。

You should also just get over the "waah learning is hard" thing and just use the form helpers as it will add the correct authenticity token:

您还应该克服“waah learning is hard”的问题并使用表单助手,因为它会添加正确的真实性令牌:

<%= form_tag("/winter", multipart: true) do %>
  <%= file_field_tag 'doc' %>
  # ...
<% end %>

And, one more thing, what if I tried get request instead of put request? get has to put the params right after the url, as a query string, right? So should then params[:doc] be always a string? I tried using get, and was sent to the url

而且,还有一件事,如果我尝试获取请求而不是放置请求怎么办?得到必须把params放在url之后,作为查询字符串,对吧?那么params [:doc]应该总是一个字符串吗?我尝试使用get,并被发送到网址

You cannot upload files in a GET request. They must be included in the request body of a POST/PATCH/PUT request with multipart/formdata encoding.

您无法在GET请求中上传文件。它们必须包含在具有multipart / formdata编码的POST / PATCH / PUT请求的请求主体中。