gpt4 book ai didi

iphone 图像到 Ruby on Rails - 内容主体错误,parse_multipart

转载 作者:行者123 更新时间:2023-12-03 19:25:35 25 4
gpt4 key购买 nike

我尝试将图像从 iPhone 上传到 Ruby on Rails (RoR) 服务器,但收到以下错误:

!\ FAILSAFE /!\  Thu Nov 11 23:51:39 CET 2010
Status: 500 Internal Server Error
bad content body
/usr/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:319:in `parse_multipart'

当我省略下面用 multipart/form-data 设置字符串值的行时,与 RoR 站点的通信工作正常。

iPhone 代码:

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

NSString *urlString = [NSString stringWithFormat:@"%@/user/iphone_results_to_website/",
serverString ];
NSMutableURLRequest *imageRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[imageRequest setURL:[NSURL URLWithString:urlString]];


[imageRequest setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"--------------------------- 14737809831466499882746641449"];
// Works fine without this next line:
[imageRequest setValue: [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData dataWithCapacity:[imageData length] + 512];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"logo1.png\"\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];


if(theConnection) {
theConnectionData = [[NSMutableData data] retain];
// theData is an instance variable
} else {
// failed to make connection
}

在RoR方面,我安装了RMagick、ImageMagick和attachment_fu,并且我为attachment_fu设置了一个照片模型:

RoR 照片模型:

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

###########################################################################

def uploaded_picture=(picture_field)
self.name = base_part_of(picture_field.original_fielname)
self.content_type = picture_field.content_type.chomp
self.data = picture_field.read

end # uploaded_picture

###########################################################################

def base_part_of(file_name)

File.basename(file_name).gsub(/[^\w._-]/, '')

end # base_part_of

###########################################################################


end

有谁知道问题出在哪里吗?问题可能出在我的 Objective-C 代码中还是 RoR 方面?

最佳答案

看起来这是rack-1.0.1 中的一个错误:

http://jira.codehaus.org/browse/JRUBY-4892

升级到最新版本的rack需要将Rails升级到2.3.9。

不,我错了,说得太早了。我在 Rails 2.3.9 和rack-1.1.0 中仍然看到此错误。

编辑:

我必须删除这一行才能使其正常工作:

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

关于iphone 图像到 Ruby on Rails - 内容主体错误,parse_multipart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4160166/

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