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でのアップロードに関するヘルプは素晴らしいでしょう。
ありがとう。