无法上传文件 [英] Cannot upload file
本文介绍了无法上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我上传文件时遇到问题:
I have problems to upload a file:
这是我的 html:
<%= form_tag import_test_path, multipart: true do %>
<%= file_field_tag :file, :style => 'display:inline; margin-top:-10px' %>
<%= submit_tag 'Hochladen', :class => 'btn btn-sm btn-info' %>
<% end %>
首先,我尝试简单地上传它并在我的控制器中像这样处理它:
First i tried to simply upload it and to handle it in my controller like this:
def import
widgets = DBF::Table.new(params[:file], nil, 'cp1252')
w = widgets.find(6)
p = Patient.new
p.vorname = w.vorname
p.name = w.name
p.geburtsdatum = w.geburt
p.save
respond_to do |format|
format.html {redirect_to :back }
end
end
但这引发了一个错误:
no implicit conversion of ActionDispatch::Http::UploadedFile into String
in line: DBF::Table.new(params[:file], nil, 'cp1252')
接下来我尝试先生成一个临时文件:
Next i tried to generate first an Tempfile:
def import
file = Tempfile.new(params[:file])
widgets = DBF::Table.new(file, nil, 'cp1252')
w = widgets.find(6)
p = Patient.new
p.vorname = w.vorname
p.name = w.name
p.geburtsdatum = w.geburt
p.save
respond_to do |format|
format.html {redirect_to :back }
end
end
但这也证明是错误的:
unexpected prefix_suffix: #<ActionDispatch::Http::UploadedFile:0x6793d10 @tempfile=# <Tempfile:C:/Users/EMMANU~1/AppData/Local/Temp/RackMultipart20131110-6816-ogkd3i>, @original_filename="patient.DBF", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"patient.DBF\"\r\nContent-Type: application/octet-stream\r\n">
in line: file = Tempfile.new(params[:file])
我做错了什么?谢谢,祝你有美好的一天!
What do i wrong? Thanks and have a nice day!
推荐答案
Accessing params[:file]
是一个 ActionDispatch::Http::UploadedFile
它只是一个用于存储上传内容的 TmpFile 的包装器.
Accessing params[:file]
is a ActionDispatch::Http::UploadedFile
which is just a wrapper for the TmpFile that is used to store the uploaded content.
您需要从类似 IO 的对象中read
以获取内容.
You need to read
from that IO like object to get the contents.
这篇关于无法上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文