使用Dragonfly从iPhone上传图像到Rails服务器

4
我正在尝试从iPhone上传图像到配置了Dragonfly gem的Rails,但是遇到了以下错误:

Dragonfly :: TempObject必须使用字符串、文件、Tempfile、另一个TempObject或响应.tempfile的内容进行初始化。

我想知道这是否与iPhone发送的MIME类型或其他内容有关。从浏览器上传正常工作。我希望能得到任何指导,以确定问题的根源——是iPhone还是服务器。
谢谢。

你正在使用ASIHttpRequest吗? - Dex
我们正在使用ASIFormDataRequest... - picardo
1个回答

0

所以我也遇到了这个问题,不过这是如何从iPhone上传到Rails网站的方法。至于他们使用的gem可能会有问题,但下面的代码确实适用于Rails,因为我也曾经学习过如何上传图片到Rails。以下是代码:

// 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"];

然后,继续将需要与请求一起发送的所有参数添加到字典中。

// 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
                       }];

请注意,由于Web浏览器的工作方式,图像需要暂时写入文件系统。这是如何通过iPhone将图像上传到Rails网站的方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接