I saw a thread on here earlier about file uploading, particularly with pictures. How does one do the controller/view part for a file upload? I tried it, but all I could get the controller to see was the file name of the file that the user picked. Thanks, Joe
Joe Van Dyk wrote:> I saw a thread on here earlier about file uploading, particularly with pictures. > > How does one do the controller/view part for a file upload? I tried > it, but all I could get the controller to see was the file name of the > file that the user picked.There''s a nice howto here: http://wiki.rubyonrails.com/rails/show/HowtoUploadFiles Alexander.
Wow, thanks! On Wed, 12 Jan 2005 18:48:42 +0100, Alexander Staubo <alex-uTpLPxngHHI4n3JXwSlx2Q@public.gmane.org> wrote:> Joe Van Dyk wrote: > > I saw a thread on here earlier about file uploading, particularly with pictures. > > > > How does one do the controller/view part for a file upload? I tried > > it, but all I could get the controller to see was the file name of the > > file that the user picked. > > There''s a nice howto here: > > http://wiki.rubyonrails.com/rails/show/HowtoUploadFiles > > Alexander. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Wed, 12 Jan 2005 18:48:42 +0100, Alexander Staubo <alex-uTpLPxngHHI4n3JXwSlx2Q@public.gmane.org> wrote:> Joe Van Dyk wrote: > > I saw a thread on here earlier about file uploading, particularly with pictures. > > > > How does one do the controller/view part for a file upload? I tried > > it, but all I could get the controller to see was the file name of the > > file that the user picked. > > There''s a nice howto here: > > http://wiki.rubyonrails.com/rails/show/HowtoUploadFilesWhat are the benefits vs drawbacks of using the database to store images? As far as I can see: Benefits: All data contained in one place Pros: Worse for performance as more load placed on database
On Wed, 12 Jan 2005 12:26:27 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, 12 Jan 2005 18:48:42 +0100, Alexander Staubo <alex-uTpLPxngHHI4n3JXwSlx2Q@public.gmane.org> wrote: > > Joe Van Dyk wrote: > > > I saw a thread on here earlier about file uploading, particularly with pictures. > > > > > > How does one do the controller/view part for a file upload? I tried > > > it, but all I could get the controller to see was the file name of the > > > file that the user picked. > > > > There''s a nice howto here: > > > > http://wiki.rubyonrails.com/rails/show/HowtoUploadFiles > > What are the benefits vs drawbacks of using the database to store images? > > As far as I can see: > > Benefits: All data contained in one place > Pros: Worse for performance as more load placed on databaseAnd another pro: backups become larger.
On Wed, 12 Jan 2005 12:26:47 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, 12 Jan 2005 12:26:27 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On Wed, 12 Jan 2005 18:48:42 +0100, Alexander Staubo <alex-uTpLPxngHHI4n3JXwSlx2Q@public.gmane.org> wrote: > > > Joe Van Dyk wrote: > > > > I saw a thread on here earlier about file uploading, particularly with pictures. > > > > > > > > How does one do the controller/view part for a file upload? I tried > > > > it, but all I could get the controller to see was the file name of the > > > > file that the user picked. > > > > > > There''s a nice howto here: > > > > > > http://wiki.rubyonrails.com/rails/show/HowtoUploadFiles > > > > What are the benefits vs drawbacks of using the database to store images? > > > > As far as I can see: > > > > Benefits: All data contained in one place > > Pros: Worse for performance as more load placed on database > > And another pro: backups become larger. >Pros should be Cons. :( Long day.
Joe Van Dyk wrote:> On Wed, 12 Jan 2005 12:26:47 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >>On Wed, 12 Jan 2005 12:26:27 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>>On Wed, 12 Jan 2005 18:48:42 +0100, Alexander Staubo <alex-uTpLPxngHHI4n3JXwSlx2Q@public.gmane.org> wrote: >>> >>>>Joe Van Dyk wrote: >>>> >>>>>I saw a thread on here earlier about file uploading, particularly with pictures. >>>>> >>>>>How does one do the controller/view part for a file upload? I tried >>>>>it, but all I could get the controller to see was the file name of the >>>>>file that the user picked. >>>> >>>>There''s a nice howto here: >>>> >>>> http://wiki.rubyonrails.com/rails/show/HowtoUploadFiles >>> >>>What are the benefits vs drawbacks of using the database to store images? >>> >>>As far as I can see: >>> >>>Benefits: All data contained in one place >>>Pros: Worse for performance as more load placed on database >> >>And another pro: backups become larger. >> > > > Pros should be Cons. :( Long day.Tell me about it ;). But, the backups don''t necessarily become larger. If you''re keeping images out of the db and pointing to them, you still need to back them up or you could have dangling references after you restore. When I make such a decision I usually just consider what the size of the database and the number of images might be. For example, I have a scheduling app for the university cable station that keeps cover art images for movies they''re playing. This is a small DB and keeping the images in the DB really doesn''t cost me anything performance-wise. In this application I even go so far as to base64 encode the images before dumping that into the DB. That, however, can add overhead. A few months ago I interviewed for a company that had an application (PHP) that would recieve over 60,000 PDF uploads per day. Since I was just interviewing with them they didn''t tell me everything about their set up, but for this guy keeping the PDFs in his DB (they were using MySQL) was not an option for him. I am not entirely sure if MySQL can handle this or not, I don''t work with that kind of load here, but on that scale it seems that it could be an important decision. So, really, I think the scope of one''s application will weigh more on one''s decision. The day is almost over :) -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> So, really, I think the scope of one''s application will weigh more on > one''s decision.I think that''s the answer to almost everything we discuss here right? ;) -- Cheers Koz
Joe Van Dyk wrote:> What are the benefits vs drawbacks of using the database to store images? > > As far as I can see: > > Benefits: All data contained in one placeAnd it''s transactional. I also count "all data in one place" as a benefit in the context of backups.> Pros: Worse for performance as more load placed on databaseYou will definitely want to cache images, either at the web server level or within Rails/ActiveRecord itself. And even if it''s cached at the Rails level you will want to support HTTP caching, so that images already cached by the client aren''t sent. This mostly involves correctly responding to the "If-Modified-Since" header. Note that this requires the in-database image to have a last-modified timestamp associated with it. Something like: require ''time'' class PhotosController < ApplicationController ... def view @photo = Photo.find(@params["id"]) timeTest = Time.rc2822( @request.headers["If-Modified-Since"]) rescue nil if timeTest and @photo.modifiedTime > timeTest render_text '''', ''304 Not Modified'' else @response.headers[''Content-Type''] = ''image/jpeg'' @response.headers[''Content-Length''] = @photo.data.length @response.headers[''Content-Transfer-Encoding''] = ''binary'' @response.headers[''Content-Description''] = @photo.description render_text @photo.imageData end Now you can easily set up Apache''s mod_cache on top of Rails, which will further lessen the load on your database, fetching it out of its own cache rather than the database whenever possible. Alexander.