2011-01-31 9 views
2

iPhoneからRailsにMultipart形式で写真を送ることはできましたが、写真がRailsサーバーに届くと、正しく保存してください。私は、ファイルがiphone_photo_to_websiteが呼ばれたときに、以下のパラメータの終わりに「userfile」とRailsのサーバー上のparamsに表示思う:attachment_fuを使ってiPhoneからRailsへの写真をアップロード

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

私は、次のObjective-Cのコードを使用してiPhoneから写真を送ります

How can I upload a photo to a server with the 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]; 

Railsのサーバー上でDB /移行での写真の移行は、次のようになります:

次SO質問から私は次のコードで写真のモデルアプリ/モデル/ photo.rb持っている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 

」:

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 

とRailsのコントローラを次のコードを持っていますimage.save "が実行されると、" false "を返します。誰がどのように私はエラーがfalseを返すimage.saveを引き起こしている見つけることができる知っていますか?それとも、データベースに写真を正しく保存できるか誰にでも分かりますか?

答えて

1

私は最終的に以下のコードを使用してデータベースに画像を保存することができました。イメージはuploaded_dataフィールドを使用してPhoto.newとして作成された場合

@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 

、image.filenameが設定されます。

iPhoneのObjective-Cコードから次の行を削除して、動作させる必要がありました。

[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
関連する問題