有没有人有使用AFNetworking上传文件的完整实现。我在互联网上找到了一些代码,但是它不完整。我找到的代码在这里:
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”(返回类型默认为“id”)
在此方面的任何帮助以及通过AFNetworks上传都将是惊人的。
谢谢。
最佳答案
首先,请确保您已下载最新版本的AFNetworking。AFHTTPRequestOperation +HTTPRequestOperationWithRequest:success:failure:
已删除了几个版本。相反,您可以执行[[AFHTTPRequestOperation alloc] initWithRequest:...]
,然后使用直接属性访问器(completionBlock
)或operation.completionBlock = ^{...}
设置-setCompletionBlockWithSuccess:failure:
。请记住,完成块是在请求完成下载后执行的。
至于多部分表单块,-appendWithFileData:mimeType:name
也在不久前被删除。您想要的方法是-appendPartWithFileData:name:fileName:mimeType:
。
进行这两个更改,一切都会正常进行。