File uploads and saving blobs to MySQL database

I've been having trouble trying to get uploaded images to save correctly to a blob field in a MySQL database.

Here's my form view

VIEW:

See the attachment_fu plugin for Rails. It will do this for you :slight_smile: Or if you want to know how to do it yourself, examine the source. It can persist to a file, a database, or S3. http://svn.techno-weenie.net/projects/plugins/attachment_fu/

Yeah, I really didn't want to go that route, this should be a real simple thing, I shouldn't need a whole bloated plugin to just save a picture to a blob field. From what I can see, attachment_fu also does the rewind and read commands, that's where I got the ideas. How do I get it back into a blob from a string? since I think thats my problem...

I’m not sure, honestly. I know the plugin works. It’s far from bloated, and it’s already got tests written to prove it works, hich saves you time. I attempted to do this one time and it wasn’t worth my time to figure it out.

Perhaps someone else reading this can tell you what’s going on.

Yeah, I really didn't want to go that route, this should be a real simple thing, I shouldn't need a whole bloated plugin to just save a picture to a blob field. From what I can see, attachment_fu also does the rewind and read commands, that's where I got the ideas. How do I get it back into a blob from a string? since I think thats my problem...

Coming into this late, but this is what I do (@the_user is exactly what is sounds like and is defined elsewhere):

view --------------------------------------------------------------- <%= form_tag({:action => 'upload_avatar', :id => @the_user},               :multipart => true) %>    Please select an image to upload.    <br />    <%= file_field_tag :image_data %>    <%= submit_tag 'Upload Avatar' %> <%= end_form_tag %>

controller --------------------------------------------------------- def upload_avatar    avatar = Avatar.new(:user => @the_user)    avatar.image_data = params[:image_data]    avatar.save    flash[:avatar_errors] = avatar.errors.full_messages    redirect_to :action => 'manage_avatars' end

model (in a before hooks)---------------------------------------------------- # may be a string or a stringio data = if image_data.respond_to?(:rewind)    image_data.rewind    image_data.read else    image_data end

img = GD2::Image.load(data)

unless img.width == IMAGE_WIDTH && img.height == IMAGE_HEIGHT    img.resize!(IMAGE_WIDTH, IMAGE_HEIGHT, true) end

self.image_data = img.jpeg(75)

sql ------------------------------------------- ...

image_data | blob | YES | | NULL | |

...

My co-worker got this working using Philip's code, which is pretty much what I tried before. Actually, it's exactly the same, rewind then read. So maybe it was my localhost acting up and playing tricks on me. I appreciate all you help, and I think we can call this one closed :slight_smile:

Summary - how it should work:

model (in a before hooks)---------------------------------------------------- # may be a string or a stringio data = if image_data.respond_to?(:rewind)    image_data.rewind    image_data.read else    image_data end