4

I am trying to upload an image from iPhone to Rails, which is configured with Dragonfly gem. The problem is I keep getting this error:

Dragonfly::TempObject must be initialized with a String, a File, a Tempfile, another TempObject, or something that responds to .tempfile

I wonder if this is related to the MIME type sent from iPhone or something else. The upload works fine from a browser. I'd appreciate any kind of guidance as to where to seek the root of the problem -- iPhone or the server.

Thanks.

c69
  • 19,951
  • 7
  • 52
  • 82
picardo
  • 24,530
  • 33
  • 104
  • 151

1 Answers1

0

So I have had that issue as well however this is how you upload to a rails site from the iPhone. As for the gem they are using there might be an issue with however the code below does for sure work with rails as I have had to learn how to upload images to rails as well. Here is the code

// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];                                    
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"------WebKitFormBoundary4QuqLuM1cE5lMwCy";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithCapacity:11];
[parameters setValue:@”information” forKey:@"Information"];

And then go ahead and add all of the parameters into the dictionary that you need to send with the request.

// add params (all params are strings)
for (NSString *param in parameters) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}


//Compress the data
NSString *FileParamConstant = @"image";
// add image data
CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
int maxFileSize = 250*1024;

NSData *imageData = [[NSData alloc] initWithContentsOfFile:imagePath];

while ([imageData length] > maxFileSize && compression > maxCompression)
{
    compression -= 0.1;
    imageData = UIImageJPEGRepresentation([UIImage imageNamed:imagePath], compression);
}
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request setHTTPBody:body];

// set URL
[request setURL:[NSURL URLWithString:[[URLLibrary sharedInstance] getCreateFeedURL]]];
NSURLResponse* response;
NSError* error;

[NSURLConnection sendAsynchronousRequest:request 
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                           make gui changes saying things worked
                       }];

Note that the image needs to be written to the file system temporarily because of the way web browsers work. This is how you upload an image to a rails site from iphone though.

Tony
  • 4,609
  • 2
  • 22
  • 32