2011-01-31 87 views
2

我想我已經能夠從iPhone發送照片時使用的多部分格式軌,但一旦照片得到Rails的服務器,我無法弄清楚如何保存正確。我認爲文件中的Rails的服務器爲「userfile的」下面的參數的結尾是否存在params時顯示iphone_photo_to_website叫做:從iPhone上傳照片到Rails服務器attachment_fu

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

我使用下面的Objective-C代碼發送從iPhone的照片從下面的SO問題:

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服務器數據庫中的照片遷移/遷移的樣子:

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 

在鐵軌上的服務器我有以下代碼的照片模型的應用程序/模型/ 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 

和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 

當「 image.save「執行,它返回」false「。有誰知道我怎麼能找到什麼錯誤導致image.save返回false?或者是否有人知道我如何在數據庫中正確保存照片?

回答

1

我終於能夠將圖像保存到使用下面的代碼數據庫:

@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 

當圖像被作爲Photo.new使用uploaded_data領域創建的,則image.filename被設定。

我也不得不刪除從iPhone上的Objective-C代碼以下行得到的東西的工作。

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