2012-04-19 10 views
0

私はクライアントサーバーアプリケーションを開発しています。 1つの画像をアップロードする方法をいくつかチュートリアルで見つけました。しかし、単一のリクエストを使用して複数の画像をアップロードするにはどうすればよいでしょうか?私はWebプログラミングでは新しいので、一般的な方法はありません。iOSでHTTPリクエストで複数の画像をアップロードするには?

助けてください。

答えて

1

ASIHTTPRequestライブラリを使用する必要があります。

次に、独自のクラスを作成して、必要なすべてのリクエストを管理します。これは私のものです:

#import "ASIFormDataRequest.h" 

@interface PostRequest : NSObject { 
    id localCopy; // to avoid exec_bad_access with arc 
    ASIHTTPRequest *getRequest; 
    ASIFormDataRequest *postRequest; 
} 

@property (nonatomic, retain) id delegate; 
@property (nonatomic, readwrite) SEL callback; 
@property (nonatomic, readwrite) SEL errorCallback; 

- (void)performGetRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector; 
- (void)performPostRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary dataDictionary:(NSDictionary *)dataDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector; 

@end 

//

#import "PostRequest.h" 

@implementation PostRequest 

@synthesize delegate; 
@synthesize callback, errorCallback; 

- (void)performGetRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector { 

    localCopy = self; 

    self.delegate = requestDelegate; 
    self.callback = requestSelector; 
    self.errorCallback = errorSelector; 

    NSMutableString *requestStringData = [[NSMutableString alloc] init]; 
    if (stringDictionary) 
     for (NSString *key in [stringDictionary allKeys]) 
      [requestStringData appendFormat:@"%@=%@&", key, [stringDictionary objectForKey:key]]; 
    NSString *resultString = [requestStringData substringToIndex:[requestStringData length]-1]; 

    getRequest = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", string, [resultString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]]; 
    [getRequest setDelegate:self]; 
    [getRequest setRequestMethod:@"GET"]; 

    //NSLog(@"request url = %@", [getRequest.url absoluteString]); 
    [getRequest startAsynchronous]; 
} 

- (void)performPostRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary dataDictionary:(NSDictionary *)dataDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector { 

    localCopy = self; 

    self.delegate = requestDelegate; 
    self.callback = requestSelector; 
    self.errorCallback = errorSelector; 

    NSURL *url = [NSURL URLWithString:string]; 

    postRequest = [[ASIFormDataRequest alloc] initWithURL:url]; 
    [postRequest setDelegate:self]; 
    [postRequest setRequestMethod:@"POST"]; 

    if (stringDictionary) 
     for (NSString *key in [stringDictionary allKeys]) 
      [postRequest setPostValue:[stringDictionary objectForKey:key] forKey:key]; 

    if (dataDictionary) 
     for (NSString *key in [dataDictionary allKeys]) 
      [postRequest setData:[dataDictionary objectForKey:key] forKey:key]; 

    //NSLog(@"request url = %@", [postRequest.url absoluteString]); 
    [postRequest startAsynchronous]; 
} 

#pragma mark - ASIHTTPRequest Delegate Implementation 

- (void)requestFinished:(ASIHTTPRequest *)crequest { 
    NSString *status = [crequest responseString]; 

    if (self.delegate && self.callback) { 
     if([self.delegate respondsToSelector:self.callback]) 
      [self.delegate performSelectorOnMainThread:self.callback withObject:status waitUntilDone:YES]; 
     else 
      NSLog(@"No response from delegate"); 
    } 
    localCopy = nil; 
} 
- (void)requestFailed:(ASIHTTPRequest *)crequest { 
    if (self.delegate && self.errorCallback) { 
     if([self.delegate respondsToSelector:self.errorCallback]) 
      [self.delegate performSelectorOnMainThread:self.errorCallback withObject:crequest.error waitUntilDone:YES]; 
     else 
      NSLog(@"No response from delegate"); 
    } 
    localCopy = nil; 
} 

@end 

それを使用するには、ちょうどあなたのUIViewControllerPostRequest.hをインポートし、同様になめらかん:

[requestManager performGetRequestWithString:tempString stringDictionary:stringDictionary dataDictionary:dataDictionary delegate:self requestSelector:@selector(requestSucceeded:) errorSelector:@selector(requestFailed:)]; 

パラメータ:

  • (NSString *)string - url striあなたのリクエストを投稿する場所。
  • (NSDictionary *)stringDictionary - すべてのテキスト情報(名前、IDなど)を含む辞書。
  • (NSDictionary *)dataDictionary - すべてのデータ情報(写真、ファイルなど)を含む辞書。
  • (id)requestDelegate - 以下のセレクタを実行する代理人。
  • (SEL)requestSelector - 正常に要求されている間に実行されるセレクタ。
  • (SEL)errorSelector - エラーが発生している間に実行されるセレクタ。
+0

魔法のように動作します!)ありがとう! – Lloyd18

1

私が使用して示唆しているいずれかASIHTTPRequestまたはAFNetworking

の両方が使用できるネットワークキューを持っています。 ASIHTTPは開発中ではありませんので、開発中のライブラリを使用したい場合はAFNetworkingをご覧ください。

+0

両方のライブラリは1つのリクエストで複数の画像をアップロードでき、商用目的に適していますか? – Lloyd18

+0

キューに画像や要求を追加できます。はい、両方のライブラリを商用アプリケーションで使用することができます – Lefteris

関連する問題