gpt4 book ai didi

iphone - 使用 Attachment_fu 将照片从 iPhone 上传到 Rails 服务器

转载 作者:行者123 更新时间:2023-12-03 21:20:58 24 4
gpt4 key购买 nike

我想我已经能够使用 Multipart 格式将照片从 iPhone 发送到 Rails,但是一旦照片到达 Rails 服务器,我不知道如何正确保存它。我认为当调用 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>}

我使用以下 SO 问题中的 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/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

在 Rails 服务器上,我有一个照片模型 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

Rails Controller 有以下代码:

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 的错误?或者有谁知道如何将照片正确保存在数据库中?

最佳答案

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

  @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_data 字段将图像创建为 Photo.new 时,image.filename 就会被设置。

我还必须从 iPhone 上的 Objective-C 代码中删除以下行才能正常工作。

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

关于iphone - 使用 Attachment_fu 将照片从 iPhone 上传到 Rails 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4854461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com