【问题标题】:How to upload a text file and parse contents into database in RoR如何在 RoR 中上传文本文件并将内容解析到数据库中
【发布时间】:2012-05-03 06:19:58
【问题描述】:

到目前为止,我已经成功上传了一个文件:

# In new.html.erb
<%= file_field_tag 'upload[file]' %>

并访问控制器中的文件

# In controller#create
@text = params[:upload][:file]

但是,这给我的只是文件名,而不是文件的内容。如何访问其内容?

我知道这是一个跳跃,但是一旦我可以访问文件的内容,是否可以上传文件夹并遍历文件?

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    完整示例

    以上传包含联系人的导入文件为例。您无需存储此导入文件,只需对其进行处理并丢弃即可。

    路线

    routes.rb

    resources :contacts do 
      collection do
        get 'import/new', to: :new_import  # import_new_contacts_path
    
        post :import                       # import_contacts_path
      end
    end
    

    表格

    views/contacts/new_import.html.erb

    <%= form_for @contacts, url: import_contacts_path, html: { multipart: true } do |f| %>
    
      <%= f.file_field :import_file %>
    
    <% end %>
    

    控制器

    controllers/contacts_controller.rb

    def new_import
    end
    
    def import
      begin
        Contact.import( params[:contacts][:import_file] ) 
    
        flash[:success] = "<strong>Contacts Imported!</strong>"
    
        redirect_to contacts_path
    
      rescue => exception 
        flash[:error] = "There was a problem importing that contacts file.<br>
          <strong>#{exception.message}</strong><br>"
    
        redirect_to import_new_contacts_path
      end
    end
    

    联系模特

    models/contact.rb

    def import import_file 
      File.foreach( import_file.path ).with_index do |line, index| 
    
        # Process each line.
    
        # For any errors just raise an error with a message like this: 
        #   raise "There is a duplicate in row #{index + 1}."
        # And your controller will redirect the user and show a flash message.
    
      end
    end
    

    希望有帮助!

    约书亚

    【讨论】:

    • 刚回来想这个。我想存储重要文件根本不重要,即使是暂时的,因为如果导入出现问题,他们无论如何都必须重新导入一个新的、固定的文件......
    【解决方案2】:

    在new.html.erb中

    <%= form_tag '/controller/method_name', :multipart => true do %>
       <label for="file">Upload text File</label> <%= file_field_tag "file" %>
       <%= submit_tag %>
    <% end %>
    

    在控制器#method_name 中

    uploaded_file = params[:file]
    file_content = uploaded_file.read
    puts file_content
    

    在 rails http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm 中查看更多文件上传 How to read whole file in Ruby?

    希望这会对你有所帮助。

    【讨论】:

    • 这给了我一个错误:undefined method 'read' for "myfile.txt":String
    • 尝试使用 :accept 选项,例如file_field_tag '文件', :accept => '文本/html'
    • 只需使用以下命令安装 oen-uri:$ sudo gem install open-uri
    • @novemberkilo 这将只允许我添加 .html 文件,并且我得到与 .html 文件相同的未定义方法错误。 Suvankar,我已经在该控制器中安装并需要 open-uri。知道我还能做什么吗?
    • 如果文件上传正常,它可以上传任何类型的文件。你错过了'multipart => true'吗?查看更多tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm
    猜你喜欢
    • 2012-05-27
    • 1970-01-01
    • 2014-04-12
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    相关资源
    最近更新 更多