从新写入的TempFile读取数据不起作用? Ruby on Rails 中的 IOerror 关闭流
我正在 Ruby on Rails 中实现,我有 2 个视图。在我的第一个视图中,我读入一个文件并显示第一行(这工作正常),然后在我的第二个视图中(在控制器中的方法中)我编写一个临时文件。一样。我想再次显示这个临时文件的内容。但这不起作用,我总是收到错误:
IOError in Project importerController#result
closed stream
这是我在控制器中的代码,在我执行的第一个视图后面:
tmpfile = Tempfile.new("tempfile")
if tmpfile
tmpfile.write(@samples)
tmpfile.close
tmpfilename = File.basename(tmpfile.path)
tmpfilenametest = tmpfilename
if !$tmpfiles
$tmpfiles = Hash.new
end
$tmpfiles[tmpfilename] = tmpfile
else
flash[:error] = "Cannot save import file."
return
end
@path = tmpfile.path
session[:importer_tmpfile] = tmpfilename
end
然后在下一个视图中,我想通过以下方式读取此文件:
tmpfilename = session[:importer_tmpfile]
i = 0
sample_count = 5
if tmpfilename
tmpfile = $tmpfiles[tmpfilename]
if tmpfile == nil
flash[:error] = "Tijdelijk bestand bestaat niet!"
return
end
end
fields_map = params[:fields_map]
attrs_map = fields_map.invert
@parsed_tempfile=CSV::Reader.parse(tmpfile)
@sampletemp = []
@parsed_tempfile.each do |row|
@sampletemp[i] = row
i = i+1
end
在这个视图中我只想展示出来,我只是:
<ul>
<% @sampletemp.each do |x| %>
<li>
<%= x %>
</li>
<% end %>
</ul>
有人看到我做错了什么吗?提前致谢!
I am implementing in Ruby on Rails, and i have 2 views. In my first view i read in a file and show the first rows (this works fine), then in my second view ( in the method in the controller ) i write a temporary file. Just the same. And i want to show the content of this temporary file again. But this doesn't work, I always get the error :
IOError in Project importerController#result
closed stream
this is my code in the controller, behind the first view i do:
tmpfile = Tempfile.new("tempfile")
if tmpfile
tmpfile.write(@samples)
tmpfile.close
tmpfilename = File.basename(tmpfile.path)
tmpfilenametest = tmpfilename
if !$tmpfiles
$tmpfiles = Hash.new
end
$tmpfiles[tmpfilename] = tmpfile
else
flash[:error] = "Cannot save import file."
return
end
@path = tmpfile.path
session[:importer_tmpfile] = tmpfilename
end
and then in my next view , I want to read this file by:
tmpfilename = session[:importer_tmpfile]
i = 0
sample_count = 5
if tmpfilename
tmpfile = $tmpfiles[tmpfilename]
if tmpfile == nil
flash[:error] = "Tijdelijk bestand bestaat niet!"
return
end
end
fields_map = params[:fields_map]
attrs_map = fields_map.invert
@parsed_tempfile=CSV::Reader.parse(tmpfile)
@sampletemp = []
@parsed_tempfile.each do |row|
@sampletemp[i] = row
i = i+1
end
In this view i just want to show it, i just have:
<ul>
<% @sampletemp.each do |x| %>
<li>
<%= x %>
</li>
<% end %>
</ul>
Does anybody see what i am doing wrong? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AFAIK,临时文件仅持续到您关闭它为止 - 然后它就不再有效。如果您想将数据实际保存在关闭后仍保留的文件中 - 那么您需要使用真正的文件。
编辑;
根据我们的讨论,我认为您根本不需要临时文件。您只是显示用户为您上传的部分文件数据。您不需要真正的文件对象(临时文件或其他文件)。
用户上传一个“文件”,您可以解析 CSV(就像您所做的那样),然后您可以将其视为字符串数组。将其保存在像“@csv_lines”这样的@var中,并在您的视图中根据需要访问数组@csv_lines。
AFAIK, a Tempfile only lasts until you close it - then it is no longer valid. If you want to actually save data in a file that sticks around after you close it - then you'll need to use a real File.
Edit;
According to our discussion, I don't think you need a tempfile at all. You are just displaying parts of the file-data that the user has uploaded for you. You don't need a real file object for that (tempfile or otherwise).
The user uploads a "file" and you can parse the CSV (as you've done) then afterwards you can just treat it as an array of strings. Save it in an @var like "@csv_lines" and in your views access the array @csv_lines however you need to.