how to upload files in rails

泄露秘密 提交于 2019-12-19 04:55:32

问题


My app is a surveybuilder...it needs upload cvs files by users, Im working with rails 3.1.3., ruby 1.9.2 ande devise 1.5.3 for authentication, but I tried: http://www.jedi.be/blog/2009/04/10/rails-and-large-large-file-uploads-looking-at-the-alternatives/ but don't works for me...can anybody tell me how to (step by step...yes I'm a begginer) upload cvs files in rails 3.1.3??? thanks in advance.


回答1:


If your goal is to upload a file to a directory you shouldn't have to use Carrierwave or Paperclip. Those gems have a lot of support for image processing and extra options.

I suggest you look at the Ruby file class and the open method to be more specific. http://www.ruby-doc.org/core-1.9.3/File.html#method-c-open

Something like the following should do the trick:

# "public/csv" is the directory you want to save the files in
# upload["datafile"] is the data populated by the file input tag in your html form 
path = File.join("public/csv", upload["datafile"].original_filename)
File.open(path, "wb") { |f| f.write(upload["datafile"].read) }

Keep in mind, your public directory is accessible to the world. If you need to save these in a more private location, make sure the directory is only readable and writable by your app.

Also, if you are working with CSV files, be sure to read through the Ruby CSV class: http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html. It makes working with CSV files a breeze.




回答2:


Carrierwave (https://github.com/jnicklas/carrierwave) is pretty much the standard when it comes to files uploading.

Otherwise, here is a simplier method if you don't need a full-fledged gem: Rails 3 - upload files to public directory




回答3:


I found an excelent project in github with javascript, in rails 3.2.1, you can upload a file and save it in the database, is done with sqlite, but is very easy to change it to mysql here is the link: upload files in rails 3.2.1, javascript and sqlite



来源:https://stackoverflow.com/questions/9568753/how-to-upload-files-in-rails

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