如何读取用户上传的文件,而不将其保存到数据库 [英] how to read a User uploaded file, without saving it to the database

查看:37
本文介绍了如何读取用户上传的文件,而不将其保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够读取用户上传的 XML 文件(小于 100kb),但不必先将该文件保存到数据库中.我不需要该文件超过当前操作(它的内容被解析并添加到数据库中;但是,解析文件不是问题).由于可以使用以下命令读取本地文件:

I'd like to be able to read an XML file uploaded by the user (less than 100kb), but not have to first save that file to the database. I don't need that file past the current action (its contents get parsed and added to the database; however, parsing the file is not the problem). Since local files can be read with:

File.read("export.opml")

我想只为 :uploaded_file 创建一个 file_field,然后尝试使用

I thought about just creating a file_field for :uploaded_file, then trying to read it with

File.read(params[:uploaded_file])

但所做的只是抛出一个 TypeError(不能将 HashWithIndifferentAccess 转换为 String).我真的尝试了很多不同的东西(包括从/tmp 目录中读取),但没有一个能正常工作.

but all that does is throw a TypeError (can't convert HashWithIndifferentAccess into String). I really have tried a lot of various things (including reading from the /tmp directory as well), but could get none of them to work.

我希望我的问题的简洁不会掩盖我为尝试自己解决这个问题所做的努力,但我不想用一百种方法来不完成它来污染这个问题.非常感谢任何插话的人.

I hope the brevity of my question doesn't mask the effort I've given to try to solve this on my own, but I didn't want to pollute this question with a hundred ways of how NOT to get it done. Big thanks to anyone who chimes in.

这是我的观点:

<% form_for(:uploaded_file, @feed, :url => {:action=>'parse'}, :html=> {:multipart=>true}) do |f| %>  <p>
    <%= f.label :uploaded_file, 'Upload your file.' %><br />
    <%= f.file_field :uploaded_file %>
  </p>
  <p><%= f.submit 'upload' %></p>
<% end %>

我设置了一个自定义操作(上传)来处理 file_field 上传,提交后,它被传递给另一个自定义操作(解析)进行处理.这可能是我问题的一部分吗?

I set up a custom action (upload) which handles the file_field upload, which after submission, is passed off to another custom action (parse) for processing. Could this be a part of my problem?

推荐答案

你们很亲近.检查params[:uploaded_file]类类型,它通常应该是StringIOTempfile 对象 -- 两者都已经作为文件,可以使用各自的 read<读取/code> 方法.

You are very close. Check the class type of params[:uploaded_file], it should typically be either a StringIO or a Tempfile object -- both of which already act as files, and can be read using their respective read method(s).

为了确定(params[:uploaded_file] 的类类型可能因您使用的是 Mongrel、Passenger、Webrick 等而有所不同),您可以进行更详尽的尝试:

Just to be sure (the class type of params[:uploaded_file] may vary depending on whether you are using Mongrel, Passenger, Webrick etc.) you can do a slightly more exhaustive attempt:

# Note: use form validation to ensure that
#  params[:uploaded_file] is not null

file_data = params[:uploaded_file]
if file_data.respond_to?(:read)
  xml_contents = file_data.read
elsif file_data.respond_to?(:path)
  xml_contents = File.read(file_data.path)
else
  logger.error "Bad file_data: #{file_data.class.name}: #{file_data.inspect}"
end

如果在您的情况下,params[:uploaded_file] 是一个哈希值,请确保您没有错误地翻转 object_name调用file_field<时的方法参数/a> 在您看来,或者您的服务器没有为您提供带有 :content_type 等键的哈希值(在这种情况下,请使用 Bad file_data ... 来自 development.log/production.log 的输出.)

If, in your case, it turns out that params[:uploaded_file] is a hash, make sure that you have not mistakingly flipped the object_name and method parameters when invoking file_field in your view, or that your server is not giving you a hash with keys like :content_type etc. (in which case please comment on this post with the Bad file_data ... output from development.log/production.log.)

这篇关于如何读取用户上传的文件,而不将其保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆