1

I am trying out the Remotipart gem for uploading files via Ajax within my Rails app. I can see that the image data is being POSTed, but how do I take convert that data into an actual image on the server?

Seems like it should be easy, like I should be able to specify a default location where the images should be placed, but I can't find any reference that describes how to handle this.

I've downloaded and run the test app but it only returns the file name to the screen. As far as I can tell the file isn't saved anywhere.

I'm sure I'm missing something obvious, but what?

bergie3000
  • 1,091
  • 1
  • 13
  • 21

1 Answers1

2

I found the kernel of the answer in another SO post its first comment and I was able to adapt it to what I need.

Here's the code I am using (again, adapted from the other post):

if remotipart_submitted?
    filename = params[:item][:image].original_filename
    extension = filename.split('.').last

    image_file = Tempfile.new(filename)

    # Save to temp file
    File.open(image_file, 'wb') do |f|
        f.write params[:item][:image].read
    end

    # Now you can do your own stuff 
    # image_file.path is a string containing the path to your new file

Maybe this will help a future visitor.

Community
  • 1
  • 1
bergie3000
  • 1,091
  • 1
  • 13
  • 21