Where should I look for information on how to use Rails and Ruby to accept the upload of a file and then to save that file to the file system of my server? I''m reading the "Uploading a file" section of Agile Web Dev with Rails, but it''s geared toward saving the file in the database. Any advice? Thanks, Jeff -- Posted via http://www.ruby-forum.com/.
If I remember correctly I used the AWDR example for the front-end and did the back-end piece after reading the file handling info from the Programming Ruby book - roughly in this section. http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_io.html If you haven''t tried the Ruby Sidebar, yet. It''s a great resource. On 3/29/06, Jeff Coleman <progressions@gmail.com> wrote:> > > Where should I look for information on how to use Rails and Ruby to > accept the upload of a file and then to save that file to the file > system of my server? > > I''m reading the "Uploading a file" section of Agile Web Dev with Rails, > but it''s geared toward saving the file in the database. > > Any advice? > > Thanks, > Jeff > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060329/48ac48f4/attachment-0001.html
Steve Koppelman
2006-Mar-29 18:23 UTC
[Rails] Re: Uploading files and writing them to server?
Google for the file_column plugin. It handles many of the minutae like figuring out where in the filesystem to store the files, handling the form field gracefully when it comes to updating the database record that references it, etc., all through a one-line declaration in the model and a string (varchar) field for the filename. And if they''re image files, it''s got hooks into RMagick, ImageMagick, etc. for creating and caching resized and otherwise processed versions of the image. Not perfect but applicable to most situations and a whole lot easier than reinventing the wheel. Jeff Coleman wrote:> > Where should I look for information on how to use Rails and Ruby to > accept the upload of a file and then to save that file to the file > system of my server? > > I''m reading the "Uploading a file" section of Agile Web Dev with Rails, > but it''s geared toward saving the file in the database. > > Any advice? > > Thanks, > Jeff-- Posted via http://www.ruby-forum.com/.
Jeff Coleman
2006-Mar-29 18:36 UTC
[Rails] Re: Uploading files and writing them to server?
Thanks, Steve, that is exactly what I''m looking for! I''ve just started playing with RMagick, as well. I have a model named Image, which seems to be causing a namespace conflict. Is there a way I can resolve that without changing my model? Jeff Steve Koppelman wrote:> Google for the file_column plugin. It handles many of the minutae like > figuring out where in the filesystem to store the files, handling the > form field gracefully when it comes to updating the database record that > references it, etc., all through a one-line declaration in the model and > a string (varchar) field for the filename. And if they''re image files, > it''s got hooks into RMagick, ImageMagick, etc. for creating and caching > resized and otherwise processed versions of the image. > > Not perfect but applicable to most situations and a whole lot easier > than reinventing the wheel. > > Jeff Coleman wrote: >> >> Where should I look for information on how to use Rails and Ruby to >> accept the upload of a file and then to save that file to the file >> system of my server? >> >> I''m reading the "Uploading a file" section of Agile Web Dev with Rails, >> but it''s geared toward saving the file in the database. >> >> Any advice? >> >> Thanks, >> Jeff-- Posted via http://www.ruby-forum.com/.
On the contrary, I homebrewed file upload because I had issues with file_column. I probably wrote about 15 lines of code, most of them in the model. I''m willing to share, if anyone wants the code. Sean Cribbs Web Services - KCKCC>>> hatlessnyc@yahoo.com 3/29/2006 12:00 PM >>>Google for the file_column plugin. It handles many of the minutae like figuring out where in the filesystem to store the files, handling the form field gracefully when it comes to updating the database record that references it, etc., all through a one-line declaration in the model and a string (varchar) field for the filename. And if they''re image files, it''s got hooks into RMagick, ImageMagick, etc. for creating and caching resized and otherwise processed versions of the image. Not perfect but applicable to most situations and a whole lot easier than reinventing the wheel. Jeff Coleman wrote:> > Where should I look for information on how to use Rails and Ruby to > accept the upload of a file and then to save that file to the file > system of my server? > > I''m reading the "Uploading a file" section of Agile Web Dev with Rails, > but it''s geared toward saving the file in the database. > > Any advice? > > Thanks, > Jeff-- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060329/551a483d/attachment-0001.html
David Thomson
2006-Mar-29 19:50 UTC
[Rails] Re: Uploading files and writing them to server?
I''d be interested to see it, but why not post the code to Snippets[1]? David [1]http://www.bigbold.com/snippets/ On 29/03/06, Sean Cribbs <scribbs@kckcc.edu> wrote:> > On the contrary, I homebrewed file upload because I had issues with > file_column. I probably wrote about 15 lines of code, most of them in the > model. I''m willing to share, if anyone wants the code. > > Sean Cribbs > Web Services - KCKCC > > >>> hatlessnyc@yahoo.com 3/29/2006 12:00 PM >>> > > Google for the file_column plugin. It handles many of the minutae like > figuring out where in the filesystem to store the files, handling the > form field gracefully when it comes to updating the database record that > references it, etc., all through a one-line declaration in the model and > a string (varchar) field for the filename. And if they''re image files, > it''s got hooks into RMagick, ImageMagick, etc. for creating and caching > resized and otherwise processed versions of the image. > > Not perfect but applicable to most situations and a whole lot easier > than reinventing the wheel. > > Jeff Coleman wrote: > > > > Where should I look for information on how to use Rails and Ruby to > > accept the upload of a file and then to save that file to the file > > system of my server? > > > > I''m reading the "Uploading a file" section of Agile Web Dev with Rails, > > but it''s geared toward saving the file in the database. > > > > Any advice? > > > > Thanks, > > Jeff > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
if you wouldn''t mind that would be great On 3/29/06, Sean Cribbs <scribbs@kckcc.edu> wrote:> > On the contrary, I homebrewed file upload because I had issues with > file_column. I probably wrote about 15 lines of code, most of them in the > model. I''m willing to share, if anyone wants the code. > > Sean Cribbs > Web Services - KCKCC > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060329/16a8b698/attachment.html
Well maybe 15 lines is a bit of an exaggeration, but it''s close. Most of this comes from others'' work, but I was able to tool it to my needs. All of these lines go in the model, which for me has a :file and :content_type attributes. :file stores the complete path to the uploaded file. Be sure to change the string in path_to_file to the place where you want files stored. Also, sanitize_filename doesn''t HAVE to be private. Enjoy! Sean Cribbs Web Services - KCKCCC ---------------snip----------------------- def file=(uploaded_file) @uploaded_file = uploaded_file @filename = sanitize_filename(@uploaded_file.original_filename) write_attribute("content_type", @uploaded_file.content_type) end def after_create if !File.exists?(File.dirname(path_to_file)) Dir.mkdir(File.dirname(path_to_file)) end if @uploaded_file.instance_of?(Tempfile) FileUtils.copy(@uploaded_file.local_path, path_to_file) else File.open(self.path_to_file, "wb") { |f| f.write(@uploaded_file.read) } end write_attribute("file", path_to_file) end def after_destroy if File.exists?(self.file) File.delete(self.file) Dir.rmdir(File.dirname(self.file)) end end def path_to_file File.expand_path("#{RAILS_ROOT}/upload/#{self.id}/#{@filename}") end private def sanitize_filename(file_name) # get only the filename, not the whole path (from IE) just_filename = File.basename(file_name) # replace all none alphanumeric, underscore or perioids with underscore just_filename.gsub(/[^\w\.\_]/,''_'') end ---------------snip----------------------->>> ljw1001@gmail.com 3/29/2006 1:43 PM >>>if you wouldn''t mind that would be great On 3/29/06, Sean Cribbs <scribbs@kckcc.edu> wrote:On the contrary, I homebrewed file upload because I had issues with file_column. I probably wrote about 15 lines of code, most of them in the model. I''m willing to share, if anyone wants the code. Sean Cribbs Web Services - KCKCC -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060329/1da32202/attachment.html