有没有人拥有使用 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"];
在我的NSDataToSend中。
还有这里:
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:myRequest success:^(id object) {
NSLog(@"Success");
} failure:^(NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Fail");
}];
错误内容为:
类方法“+HTTPRequestOperationWithRequest:success:failure”未找到(返回类型默认为“id”)
对此问题的任何帮助,以及使用AFNetworks上传的经验都将是惊人的。
谢谢。
multipartFormRequestWithMethod
方法上传图片时将内容类型从multipart/form-data;
更改为image/jpeg
吗?需要您的帮助! - Almas Adilbek