【问题标题】:rails read xls uploaded filerails读取xls上传的文件
【发布时间】:2014-01-23 12:51:48
【问题描述】:

我已上传 .xls 文件,但 roo 无法读取它。 在控制器中我有:

  def create
    name = params[:upload][:file].original_filename
    directory = "public/uploads"
    path = File.join(directory, name)
    file =  File.open(path, "wb") { |f| f.write(params[:upload][:file].read) }
    flash[:notice] = "File uploaded"
    file.inspect
    Product.import(params[:upload][:file].original_filename)    
    redirect_to upload_path
  end

在模型中:

  def self.import(file)

  spreadsheet = open_spreadsheet(file)
 end


  def self.open_spreadsheet(upload)
    directory = "public/uploads"
    file = File.join(directory, upload)
    case File.extname(file.original_filename)
    when ".csv" then Csv.new(file.path, nil, :ignore)
    when ".xls" then Excel.new(file.path, nil, :ignore)
    when ".xlsx" then Excelx.new(file.path, nil, :ignore)
    else raise "Unknown file type: #{file.original_filename}"
    end
  end

但是上传变量我有字符串,我不能使用文件方法。如何打开已上传的文件以供 roo gem 阅读?

【问题讨论】:

    标签: ruby-on-rails xls


    【解决方案1】:

    我在这里描述了如何在不使用 gems 的情况下处理上传:

    https://stackoverflow.com/a/9545030/1001324

    你不能像你那样在服务端使用原始文件名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      相关资源
      最近更新 更多