简体   繁体   English

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

[英]Upload Photo from iPhone to Rails server with attachment_fu

I think I've been able to send a photo from the iPhone to Rails using the Multipart format, but once the photo gets to the Rails server, I can't figure out how to save it correctly. 我认为我已经能够使用Multipart格式将照片从iPhone发送到Rails,但是一旦照片到达Rails服务器,我就不知道如何正确保存它。 I think the file shows up in the params on the Rails server as "userfile" at the end of the Parameters below when iphone_photo_to_website is called: 我认为在调用iphone_photo_to_website时,该文件在以下参数末尾的Rails服务器上的参数中显示为“ userfile”:

Parameters: {"action"=>"iphone_photo_to_website","id"=>"8A39FA8F_1ADD_52AB_8387_E1C5CFC4AB2D", "controller"=>"users", "userfile"=>#<File:/tmp/RackMultipart30053-0>}

I send the photo from the iPhone using the following Objective-C code from the following SO question: 我使用来自以下SO问题的以下Objective-C代码从iPhone发送照片:

How can I upload a photo to a server with the iPhone? 如何使用iPhone将照片上传到服务器?

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"logo1" ofType:@"png"];
NSData *imageData   = [NSData dataWithContentsOfFile:imagePath];

NSString *urlString = [NSString stringWithFormat:@"%@/users/iphone_photo_to_website/%@",
                    serverString, UID];


NSMutableURLRequest *imageRequest = [[NSMutableURLRequest alloc] init] ;
[imageRequest setURL:[NSURL URLWithString:urlString]];
[imageRequest setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[imageRequest setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData dataWithCapacity:[imageData length] + 512];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"logo1.png\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[imageRequest setHTTPBody:body];

theConnection = [[NSURLConnection alloc] initWithRequest:imageRequest 
                                            delegate:self];

On the Rails server the photo migration in db/migrate looks like: 在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

On the Rails server I have a Photo model app/models/photo.rb with the following code: 在Rails服务器上,我有一个包含以下代码的Photo模型app / models / photo.rb:

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

And the Rails controller has the following code: Rails控制器具有以下代码:

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

When "image.save" executes, it returns "false". 执行“ image.save”时,返回“ false”。 Does anyone know how I can find what error is causing image.save to return false? 有谁知道我如何找到导致image.save返回false的错误? Or does anyone know how I can save the photo correctly in the database? 还是有人知道我如何将照片正确保存在数据库中?

I was finally able to save the image to the database using the following code: 我终于可以使用以下代码将图像保存到数据库:

  @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

When the image is created as a Photo.new using the uploaded_data field, the image.filename gets set. 使用upload_data字段将图像创建为Photo.new时,将设置image.filename。

I also had to remove the following line from the Objective-C code on the iPhone to get things to work. 我还必须从iPhone上的Objective-C代码中删除以下行,以使一切正常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM