2012-04-19 74 views
0

我正在開發客戶端服務器應用程序。我找到了幾個教程如何上傳一張圖片。但是,使用單個請求上傳多個圖像的最佳方式是什麼?我是網絡編程的新手,所以我不是常用的方法。如何在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 

要使用它,只需要導入PostRequest.hUIViewController,做水木清華這樣的:

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

參數:

  • (NSString *)string - 網址條紋ng,在哪裏發佈您的請求;
  • (NSDictionary *)stringDictionary - 字典,其中包含所有的文本信息(如名稱,ID等);
  • (NSDictionary *)dataDictionary - 字典,其中包含所有數據信息(如照片,文件等);
  • (id)requestDelegate - 代表執行下面的選擇器;
  • (SEL)requestSelector - 選擇器,將在成功請求時執行;
  • (SEL)errorSelector - 選擇器,將在發生錯誤時執行。
+0

像魔術一樣工作!)謝謝! – Lloyd18 2012-04-20 09:39:27

1

我會建議使用兩種ASIHTTPRequestAFNetworking

兩者都有,您可以使用網絡隊列。 ASIHTTP不再被開發,因此如果您想使用正在開發的庫,您可能會考慮AFNetworking ...

+0

這兩個庫都可以在一個請求中上傳多個圖像並適合商業使用? – Lloyd18 2012-04-19 11:46:01

+0

您可以在隊列中添加圖片或請求。是的,這兩個庫都可以用於商業應用 – Lefteris 2012-04-19 11:47:40