jh100000
2006-Jul-26 17:01 UTC
[Rails] code for uploading files to the filesystem instead of db
Does anyone have any standard code for uploading files to the filesystem? I''ve got code already for uploading images to the database but I''d prefer to store the files on the filesystem. In particular I need to know how to (a) ensure a file has been selected in the file_field (b) how to extract the filename from the file_field (c) how to actually move the file from client to server --John -- Posted via http://www.ruby-forum.com/.
jh100000
2006-Jul-26 17:01 UTC
[Rails] Re: code for uploading files to the filesystem instead of db
Josh Peek wrote:> Check out acts as attachment. > > http://svn.techno-weenie.net/projects/plugins/acts_as_attachment/Problem is this requires RMagick - I can''t seem to get this to work on my machine - don''t know why. My production server arrives tomorrow so I can try again with that. Don''t suppose anyone has a solution that doesn''t need any other plugins? --John -- Posted via http://www.ruby-forum.com/.
Josh Peek
2006-Jul-26 17:02 UTC
[Rails] Re: code for uploading files to the filesystem instead of db
Check out acts as attachment. http://svn.techno-weenie.net/projects/plugins/acts_as_attachment/ -- Posted via http://www.ruby-forum.com/.
Josh Peek
2006-Jul-26 17:18 UTC
[Rails] Re: code for uploading files to the filesystem instead of db
> Problem is this requires RMagickOnly to resize images. -- Posted via http://www.ruby-forum.com/.
jh100000
2006-Jul-26 18:22 UTC
[Rails] Re: code for uploading files to the filesystem instead of db
For anyone who is interested, I found this really simple way of uploading a file to the filesystem without the need for any plugins or any complicated code. Takes 10 minutes to get it going. http://www.forthecode.com/user/comment/15 --John -- Posted via http://www.ruby-forum.com/.
Alex Wayne
2006-Jul-26 18:50 UTC
[Rails] Re: code for uploading files to the filesystem instead of db
jh100000 wrote:> > Does anyone have any standard code for uploading files to the > filesystem? > > I''ve got code already for uploading images to the database but I''d > prefer to store the files on the filesystem. > > In particular I need to know how to > > (a) ensure a file has been selected in the file_field > (b) how to extract the filename from the file_field > (c) how to actually move the file from client to server > > > --JohnNot too hard #view <%= form_tag({:action => ''upload''}, {:multipart => true}) %> <%= file_field_tag ''my_file'' %> <%= end_form_tag %> #controller def upload file = params[:my_file] if file.size > 0 File.open("#{RAILS_ROOT}/some/dir/#{file.original_filename}", ''w+'') do |f| f.write(file.read) end end end a) "file.size > 0" tests whether or not there a file was actually uploaded b) "file.original_filename" retrieves the name of the file c) the directory location provided to File.open is where the uploaded file will be written. Just make sure permissions are set properly in the directory you are uploading to and that should be all you need. -- Posted via http://www.ruby-forum.com/.