Search code examples
iosobjective-cnsurlrequest

How to upload multiple images in ios?


I have tried this code to implement multiple images in iOS. Didn't work as I wanted:

NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";

// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ
NSString* FileParamConstant = @"fileToUpload[]";



NSArray *arrayfile=[NSArray arrayWithObjects:FileParamConstant,FileParamConstant ,nil];
// the server url to which the image (or the media) is uploaded. Use your server url here
NSURL* requestURL = [NSURL URLWithString:@"http://jackson.amtpl.in/tranzop/api/customertest/postorder"];

// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

// add params (all params are strings)
for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
for (int i=0; i<upload_img_array.count; i++) {
   NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:[upload_img_array objectAtIndex:0]],1);    // add image data
// NSData *imageData = UIImageJPEGRepresentation([upload_img_array objectAtIndex:i], 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image%d.png\"\r\n", FileParamConstant,i] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
}
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];

// set

Solution

  • try

    //img_send     is a UIImage with u want to send
         NSData *imageData = UIImagePNGRepresentation(img_send);
    
    
    
    
    
                             str_img= [imageData base64EncodedStringWithOptions:0];
    
    
    
    
    
    
    
    
    
                    NSDictionary *post_dir = [[NSMutableDictionary alloc] init];
    
    
    
                    [post_dir setValue:str_img forKey:@"image_string"];
    
    
    
                    NSData *jsData = [NSJSONSerialization dataWithJSONObject:post_dir options:NSJSONWritingPrettyPrinted error:nil];
    
    
    
                    NSMutableData *data = [[NSMutableData alloc] init];
    
                    self.receivedData = data;
    
    
    
    
    
                    NSURL *url = [NSURL URLWithString:@"YOUR URL STRING"];
    
    
    
                    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
    
    
    
                    //set http method
    
                    [request setHTTPMethod:@"POST"];
    
                    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
                    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
                    [request setValue:[NSString stringWithFormat:@"%d", [jsData length]] forHTTPHeaderField:@"Content-Length"];
    
                    [request setHTTPBody:jsData];
    
    
    
                    //initialize a connection from request
    
                    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
                    self.connection=connection;
    
    
    
    
    
                    //start the connection
    
                    [connection start];
    
    
    
                    NSHTTPURLResponse *response = nil;
    
                    NSError *error = nil;
    
    
    
    
    
                    NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
                    NSLog(@"~~~~~ Status code: %d", [response statusCode]);
    
                });