Iphone image to Ruby on Rails - bad content, parse_multipart

I am trying to upload an image from iPhone to a Ruby on Rails (RoR) server, and I get the following error:

!\ 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' 

Linking to the RoR site works fine when I leave the line below, where I set the value for the line with multipart / form-data.

IPhone Code:

  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 } 

On the RoR side, I have RMagick, ImageMagick and attachment_fu installed, and I set the photo model for attachment_fu as:

RoR Fashion Model:

 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 

Does anyone know what the problem is? Could the problem be in my Objective-C code or on the RoR side?

+4
source share
2 answers

It looks like it was a bug in rack-1.0.1:

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

Upgrading to the latest rack version requires updating Rails to 2.3.9.

No, I was mistaken, spoke too early. I still see this error with Rails 2.3.9 and rack-1.1.0.

Edit:

I had to delete this line to make it work:

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

I just wanted to trace and say that the first did not help me, but I had a similar problem. The following example worked. I ended up commenting on a similar line.

http://www.travisdunn.com/posting-multipart-file-uploads-on-the-iphone

0
source

All Articles