gpt4 book ai didi

iphone - 从 iPhone 将图像上传到 Rails 服务器(使用 Dragonfly)

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

我正在尝试将图像从 iPhone 上传到 Rails,该图像配置为 Dragonfly gem.问题是我不断收到此错误:

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

我想知道这是否与从iPhone或其他设备发送的MIME类型有关。从浏览器上传工作正常。我希望能得到任何关于在哪里寻找问题根源的指导——iPhone还是服务器。

谢谢。

最佳答案

所以我也遇到过这个问题,但是这是从 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
}];

请注意,由于网络浏览器的工作方式,图像需要临时写入文件系统。这就是从 iPhone 将图像上传到 Rails 站点的方法。

关于iphone - 从 iPhone 将图像上传到 Rails 服务器(使用 Dragonfly),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7135733/

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