使用附件将照片从iPhone上传到Rails服务器

使用附件将照片从iPhone上传到Rails服务器,iphone,ruby-on-rails,objective-c,photos,attachment-fu,Iphone,Ruby On Rails,Objective C,Photos,Attachment Fu,我想我已经能够使用多部分格式将照片从iPhone发送到Rails,但是一旦照片到达Rails服务器,我就无法正确地保存它。我认为当调用iphone_photo_to_网站时,Rails服务器上的参数中会显示以下参数末尾的“userfile”文件: Parameters: {"action"=>"iphone_photo_to_website","id"=>"8A39FA8F_1ADD_52AB_8387_E1C5CFC4AB2D", "controller"=>"users",

我想我已经能够使用多部分格式将照片从iPhone发送到Rails,但是一旦照片到达Rails服务器,我就无法正确地保存它。我认为当调用iphone_photo_to_网站时,Rails服务器上的参数中会显示以下参数末尾的“userfile”文件:

Parameters: {"action"=>"iphone_photo_to_website","id"=>"8A39FA8F_1ADD_52AB_8387_E1C5CFC4AB2D", "controller"=>"users", "userfile"=>#<File:/tmp/RackMultipart30053-0>}
在Rails服务器上,照片迁移(以db/migrate为单位)如下所示:

class CreatePhotos < ActiveRecord::Migration
  def self.up
    create_table :photos do |t|

      t.column :user_id, :integer
      t.column :title, :string
      t.column :body, :text
      t.column :created_at, :datetime

#  the following are for attachment_fu

      t.column :photo, :binary
      t.column :content_type, :string
      t.column :filename, :string
      t.column :size, :integer
      t.column :parent_id, :integer
      t.column :thumbnail, :string
      t.column :width, :integer
      t.column :height, :integer


    end
  end

  def self.down
    drop_table :photos
  end
end

执行“image.save”时,返回“false”。有人知道我如何找到导致image.save返回false的错误吗?或者有人知道如何将照片正确保存到数据库中吗?

我终于能够使用以下代码将图像保存到数据库中:

class Photo < ActiveRecord::Base

    has_attachment :storage       => :file_system,
                   :resize_to     => '640x480',
                   :thumbnails    => { :thumb => '160x120', :tiny => '50>' },
                   :max_size      => 5.megabytes,
                   :content_type  => :image,
                   :processor     => 'Rmagick'
    validates_as_attachment
    belongs_to :user
end
def iphone_photo_to_website
    @udid          = params[:id]
    @userfile      = params[:userfile]

  @photo_params = { :user_id               => @user.id,
                    :photo                 => @userfile,
                    :content_type          => 'image',
                    :filename              => @userfile.original_path,
                    :uploaded_data         => @userfile
                  }
 image = Photo.new(@photo_params)
 image.save


end
  @userfile      = params[:userfile]

  image = Photo.new(:uploaded_data => params[:userfile])

  image.user_id          = @user.id
  image.photo            = @userfile
  image.width            = 105
  image.height           = 104
  basename               = File.basename(image.filename).gsub(/[^\w._-]/, '')
  image.content_type     = "image/" + basename.split("\.")[1]

  image.save
当使用uploaded_数据字段将图像创建为Photo.new时,会设置image.filename

我还必须从iPhone上的Objective-C代码中删除以下代码行,才能让事情正常进行

 [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];