Heroku file upload problem

允我心安 提交于 2019-11-29 00:42:43

Based on Avishal's answer I use this with Rails 3:

@file = IO.read(params[:report].tempfile.path)

Not entirely true, actually. It turns out that if you upload a file to Heroku, you can access it for the duration of the request via the Tempfile class. I was able to read the tempfile into a string and then process it as needed (it's a CSV):

@file = IO.read(params[:report][:file].path)

Works fine for text/csv files, but I guess if you want to do more than basic text manipulation you have to go with S3 as Joost suggests.

I am still suck with this problem. I tried the solution given here to be able to do a csv file upload and then parse it to fill my DB through acive record. I have the following code:

file = IO.read(params[:file].tempfile.path)
    FasterCSV.new(file, :headers => true).each do |row|
         # my parsing logic
    end

The code just works perfect in the local but does not work at all in Heruko. I get error and all that I could see in the heruko logs is this:

Started POST "/projects/1/upload_pivotal_csv" for 122.172.25.106 at 2012-03-23 07:45:59 +0000 2012-03-23T07:46:00+00:00 app[web.1]: 2012-03-23T07:46:00+00:00 app[web.1]: NotImplementedError (Please switch to Ruby 1.9's standard CSV library. It's FasterCSV plus support for Ruby 1.9's m17n encoding engine.): 2012-03-23T07:46:00+00:00 app[web.1]: app/controllers/projects_controller.rb:17:in `upload_pivotal_csv' 2012-03-23T07:46:00+00:00 app[web.1]: 2012-03-23T07:46:00+00:00 app[web.1]: 2012-03-23T07:46:00+00:00 heroku[router]: POST castletrack.herokuapp.com/projects/1/upload_pivotal_csv dyno=web.1 queue=0 wait=0ms service=783ms status=500 bytes=728 2012-03-23T07:46:00+00:00 app[web.1]: Processing by ProjectsController#upload_pivotal_csv as HTML 2012-03-23T07:46:00+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"sBmRWpGP3q9Hu7O2cMlmnGTByaTXValxYHw5+cFoSw0=", "file"=>#>, "commit"=>"Import", "id"=>"1"} 2012-03-23T07:46:00+00:00 app[web.1]: Completed in 406ms

I am sure it has something to do with the ruby version. I am using ruby 1.8.7 locally and its a rails 3 application. I am not sure what ruby version is there on heruko. But I can tell that I am using ceder stack.

Yep. the file system is read-only. You can store your files on S3.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!