我在获取一些我已经获得的代码时遇到了一些麻烦,我对php / html的体验充其量只是“有限”,所以我希望通过询问这一点,我看起来不像太多的菜鸟。
我遇到的问题是无法将小图像上传到网络服务器。在过去的4-5个小时里,我已经玩过以下几个小时,在英国已经快到凌晨2点,我所取得的唯一进步一直在变化
[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
到
[NSString stringWithFormat:@"enctype=\"multipart/form-data\"; boundary=%@",boundary];
这使我的服务器每次发送内容时都会停止发出内部错误。
代码如下,它位于本网站的多个页面上,我找不到原作者给予的信任。当我在接收php文件中有print_r($_FILES);
时,我正在获取一个空数组。
提前感谢任何帮助,非常感谢。
NSString *urlString = @"http://mysite/upload_image.php";
UIImage * img = [UIImage imageNamed:@"mypic.png"];
NSData *imageData = UIImageJPEGRepresentation(img, 90);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"enctype=\"multipart/form-data\"; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"image\"; filename=\"davesfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"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]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
答案 0 :(得分:7)
一些反应:
如果您使用AFNetworking,创建这些请求的过程会更简单,让您自己摆脱构建请求的杂草:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:[url absoluteString] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:fieldName fileName:[imagePath lastPathComponent] mimeType:[self mimeTypeForPath:imagePath]];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Success: %@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
顺便说一句,我通常使用MobileCoreServices.framework来获取基于扩展名的mime类型,而不是硬编码:
- (NSString *)mimeTypeForPath:(NSString *)path
{
// get a mime type for an extension using MobileCoreServices.framework
CFStringRef extension = (__bridge CFStringRef)[path pathExtension];
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extension, NULL);
assert(UTI != NULL);
NSString *mimetype = CFBridgingRelease(UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType));
assert(mimetype != NULL);
CFRelease(UTI);
return mimetype;
}
我注意到您正在将图片检索到UIImage
,然后使用UIImageJPEGRepresentation
获取NSData
。如果您愿意或必须这样做,但我通常更喜欢发送原始数据,直接绕过UIImage
的往返,这会导致图像质量下降和元数据丢失。相反,您可能想直接抓取NSData
:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"mypic" ofType:@"png"]; // note, I'm going back to bundle, not grabbing `UIImage` from imageview
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
如果你真的必须通过UIImage
进行往返,那么你可能想要使用imageWithContentsOfFile
而不是imageNamed
,因为后者会不必要地缓存图像。
如果您决定自己创建请求,这是我过去使用的例程:
- (void)uploadFileAtPath:(NSString *)imagePath
forField:(NSString *)fieldName
URL:(NSURL*)url
parameters:(NSDictionary *)parameters
{
NSString *filename = [imagePath lastPathComponent];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSMutableData *httpBody = [NSMutableData data];
NSString *boundary = [self generateBoundaryString];
NSString *mimetype = [self mimeTypeForPath:imagePath];
// configure the request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
// set content type
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// add params (all params are strings)
[parameters enumerateKeysAndObjectsUsingBlock:^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) {
[httpBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"%@\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
}];
// add image data
if (imageData) {
[httpBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", fieldName, filename] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", mimetype] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:imageData];
[httpBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
[httpBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:httpBody];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError)
{
NSLog(@"sendAsynchronousRequest error=%@", connectionError);
return;
}
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Success: %@", string);
}];
}
显然,如果您没有使用任何parameters
(您似乎没有这样做),则可以为该参数传递nil
。但关键是如果需要它还支持其他参数。我还使用异步网络请求,因为不应该在主队列上发出同步网络请求。
这使用Apple的SimpleURLConnections示例中的generateBoundaryString
方法:
- (NSString *)generateBoundaryString
{
// generate boundary string
//
// adapted from http://developer.apple.com/library/ios/#samplecode/SimpleURLConnections
//
// Note in iOS 6 and later, you can just:
//
// return [NSString stringWithFormat:@"Boundary-%@", [[NSUUID UUID] UUIDString]];
CFUUIDRef uuid;
NSString *uuidStr;
uuid = CFUUIDCreate(NULL);
assert(uuid != NULL);
uuidStr = CFBridgingRelease(CFUUIDCreateString(NULL, uuid));
assert(uuidStr != NULL);
CFRelease(uuid);
return [NSString stringWithFormat:@"Boundary-%@", uuidStr];
}
如果您愿意,我会根据以上代码模拟您的代码:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", fieldName, [imagePath lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", [self mimeTypeForPath:imagePath]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
就个人而言,我建议您更改PHP代码以生成JSON响应(因为这会让您的应用更容易使用)。
顺便说一下,我用我的PHP代码测试了上面的内容。如果您仍然遇到问题,我建议您与我们共享您的PHP代码,因为构造格式良好的请求与PHP代码所需的内容密切相关。