AFNetworking上传文件
是否有任何人使用AFNetworking上传文件的完整实现? 我在网上find了一些代码,但并不完整。 我find的代码在这里:
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://my.client.server.com"]]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setObject:[fieldName text] forKey:@"field01_nome"]; [parameters setObject:[fieldSurname text] forKey:@"field02_cognome"]; NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"/Contents/mail/sendToForm.jsp" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { [formData appendPartWithFileData:myNSDataToSend mimeType:@"image/jpeg" name:@"alleagto"]; }]; AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:myRequest success:^(id object) { NSLog(@"Success"); } failure:^(NSHTTPURLResponse *response, NSError *error) { NSLog(@"Fail"); }]; [operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite); }]; queue = [[[NSOperationQueue alloc] init] autorelease]; [queue addOperation:operation];
我得到以下几行错误:
[formData appendPartWithFileData:myNSDataToSend mimeType:@"image/jpeg" name:@"alleagto"];
在myNSDataToSend。
和这里:
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:myRequest success:^(id object) { NSLog(@"Success"); } failure:^(NSHTTPURLResponse *response, NSError *error) { NSLog(@"Fail"); }];
错误是:
类方法'+ HTTPRequestOperationWithRequest:success:failure'not found(返回types默认为'id')
任何帮助,并与AFNetworks上传将是惊人的。
谢谢。
首先,确保你已经下载了最新版本的AFNetworking。
AFHTTPRequestOperation +HTTPRequestOperationWithRequest:success:failure:
被删除了几个版本。 相反,你可以做[[AFHTTPRequestOperation alloc] initWithRequest:...]
,然后用直属性存取器( operation.completionBlock = ^{...}
)或者用-setCompletionBlockWithSuccess:failure:
来设置-setCompletionBlockWithSuccess:failure:
。 请记住,请求完成下载后,完成块将执行。
至于multipart表单块, -appendWithFileData:mimeType:name
也被删除了一段时间。 你想要的方法是-appendPartWithFileData:name:fileName:mimeType:
做出这两个改变,一切都应该工作。