2016-10-25 61 views
0

上傳陣列imagesstring參數到服務器
上傳圖像和字符串數組。我正在使用這種方法,但只有 我可以上傳single image這是第一個。而如果陣列數量是固定的,那麼你可以添加圖片到身體其他相應的陣列圖像不上傳如何將圖像和字符串參數數組上傳到服務器?

[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
      [request setHTTPShouldHandleCookies:NO]; 
      [request setTimeoutInterval:30]; 
      [request setHTTPMethod:action_type]; 



      NSString *boundary = @"14737809831466499882746641449"; 
      NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
      [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; 



      NSMutableData *body = [NSMutableData data]; 

    if([param isEqualToString:kprofile_pic]) 
       { 

        //Photo 
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
        [body appendData:[@"Content-Disposition: form-data; name=\"photo[]\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
        [body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
        [body appendData:[dictionary objectForKey:param]]; 
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

       } 
       else 
       { 

       [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
       [body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]]; 
       [body appendData:[[NSString stringWithFormat:@"%@", [dictionary objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]]; 
       [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

       } 


      } 


      [request setHTTPBody:body]; 

回答

0

。但是,如果數組數不固定或者是動態的,則在服務器端逐個發佈圖像。檢查以下內容:

-(void)uploadimage 
{ 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"yyyyMMddHHmmss"]; 
    NSDate *now = [[NSDate alloc] init]; 
    NSString *theDate = [dateFormat stringFromDate:now]; 
    NSLog(@"\n" 
      "theDate: |%@| ", theDate); 
    UIImage *image = [imagearray objectAtIndex:imagecount]; 
    imagecount++; 


    NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.7f)]; 


    NSURL *dataURL = [NSURL URLWithString:baseurl]; 

    NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; 

    [dataRqst setHTTPMethod:@"POST"]; 

    NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; 
    NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; 

    [dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"]; 

    NSMutableData *postBody = [NSMutableData data]; 


    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n",[NSString stringWithFormat:@"%@.png",theDate]] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:imageData]; 
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

    [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [dataRqst setHTTPBody:postBody]; 

    NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init]; 
    NSError* error = nil ; 
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:dataRqst delegate:self]; 

    } 

#pragma mark NSURLConnection Delegate Methods 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    self.receiveData = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [self.receiveData appendData:data]; 

    NSError* error; 
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data 
                 options:kNilOptions 
                  error:&error]; 
    NSLog(@"json..%@",json); 
    if (imagecount <[imagearray count]) { 
     [self uploadimage]; 
    } 
    else 
    { 

       UIAlertView *alert= [[UIAlertView alloc] initWithTitle:nil 
                   message:@"Data uploaded successfully" 
                   delegate:nil 
                 cancelButtonTitle:nil 
                 otherButtonTitles:nil, nil] ; 
       [alert show]; 
    } 

} 

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection 
        willCacheResponse:(NSCachedURLResponse*)cachedResponse { 
    return nil; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
} 

-(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite 
{ 
} 
+0

這裏,其中u上傳圖像陣列.. ??? –

+0

取一個int,假設int imagecount = 0;然後調用[self uploadimage];然後檢查 - (void)連接:(NSURLConnection *)連接didReceiveData:(NSData *)數據在這個方法中,我們檢查圖像計數是否等於數組數。 –

+0

但你追加imageData其中是[uploadimage] –

0
#import "tesstt.h" 
#import "HttpRequest.h" 
@interface tesstt() 
{ 
    int imagecount; 
} 
@property(nonatomic,retain)NSMutableData *receiveData; 
@property(nonatomic,retain)NSMutableArray *imagearray; 

@end 

@implementation tesstt 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

-(IBAction)submit:(id)sender 
{ 
    imagecount = 0; 
    [self uploadimage]; 
} 
-(void)uploadimage 
{ 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"yyyyMMddHHmmss"]; 
    NSDate *now = [[NSDate alloc] init]; 
    NSString *theDate = [dateFormat stringFromDate:now]; 
    NSLog(@"\n" 
      "theDate: |%@| ", theDate); 
    UIImage *image = [self.imagearray objectAtIndex:imagecount]; 
    imagecount++; 


    NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.7f)]; 

    NSString *baseurl = @""; 

    NSURL *dataURL = [NSURL URLWithString:baseurl]; 

    NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; 

    [dataRqst setHTTPMethod:@"POST"]; 

    NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; 
    NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; 

    [dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"]; 

    NSMutableData *postBody = [NSMutableData data]; 


    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n",[NSString stringWithFormat:@"%@.png",theDate]] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:imageData]; 
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

    [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [dataRqst setHTTPBody:postBody]; 

    NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init]; 
    NSError* error = nil ; 
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:dataRqst delegate:self]; 

} 

#pragma mark NSURLConnection Delegate Methods 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    self.receiveData = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [self.receiveData appendData:data]; 

    NSError* error; 
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data 
                 options:kNilOptions 
                  error:&error]; 
    NSLog(@"json..%@",json); 
    if (imagecount <[self.imagearray count]) { 
     [self uploadimage]; 
    } 
    else 
    { 

     UIAlertView *alert= [[UIAlertView alloc] initWithTitle:nil 
                 message:@"Data uploaded successfully" 
                 delegate:nil 
               cancelButtonTitle:nil 
               otherButtonTitles:nil, nil] ; 
     [alert show]; 
    } 

} 

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection 
        willCacheResponse:(NSCachedURLResponse*)cachedResponse { 
    return nil; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
} 

-(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite 
{ 
} 
@end 
+0

我有圖像陣列,我想單擊一次發送到服務,是烏爾代碼傳遞圖像點擊或逐一? @Minkle Garg –

相關問題