14

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

ありがとう。

4

1 に答える 1

19

まず、最新バージョンの AFNetworking がダウンロードされていることを確認してください。

AFHTTPRequestOperation +HTTPRequestOperationWithRequest:success:failure:数バージョン前に削除されました。代わりに、単純なプロパティ アクセサー ( ) または を使用して を実行[[AFHTTPRequestOperation alloc] initWithRequest:...]し、設定することができます。リクエストのダウンロードが完了した後に完了ブロックが実行されることに注意してください。completionBlockoperation.completionBlock = ^{...}-setCompletionBlockWithSuccess:failure:

マルチパート フォーム ブロック-appendWithFileData:mimeType:nameも、しばらく前に削除されました。ご希望の方法は-appendPartWithFileData:name:fileName:mimeType:.

これら 2 つの変更を行うと、すべてが機能するはずです。

于 2011-11-23T03:15:02.863 に答える