views:

16

answers:

1

Hello,

All my rails paper_clip images are being uploaded to Rails as "application/octet-stream"

Which is casuing issues.

How in Rails do I set the content/type to the right type? Image/png etc, based on the actual image being uploaded?

Thanks

+1  A: 

Well, in general you can always detect the proper mime-type from the uploaded file itself by using 'mime/types':

# Controller

def create
  @photo = Photo.new(:upload_file => params[:photo][:image])
  ...
end

# Model

class Photo < ActiveRecord::Base  
  require 'mime/types'
  ...
  def upload_file=(data)
    data.content_type = MIME::Types.type_for(data.original_filename).to_s
    self.image = data
  end 
end 
mdrozdziel
Seems to be errors? "NoMethodError (undefined method `original_filename' for #<ActiveSupport::HashWithIndifferentAccess:0x107b730b0>): app/models/photo.rb:29:in `swfupload_file=' app/controllers/photos_controller.rb:15:in `upload' app/middleware/flash_session_cookie_middleware.rb:14:in `call'"
TheExit
Are you sure, you are passing the filename as photo[image] ('fileDataName': 'photo[image]') ? If thats the case, try to running the stack in debug mode and place "debugger" as the first call in the upload_file method. Then you can inspect data object.
mdrozdziel