0

I have to choose images or videos from the iPhone library and upload those selected images and videos to the server. I looked for the multipart form upload but could not get the necessary information.

I have the following JSON structure to post.

{"uuid":"a6059eb6-2417-4575-8f83-e5eca065a1bb","id":901,"username":"somename","description":"Some Desciption","date":"Some date","title":"Some Title","published":1,"type":"Some Type","responsible":["Person 1","Person 2","Person 3"],"products_List":["Product 1"],"assets":[{"uuid":"e1102eae-987a-4930-96ad-5ae331d785bc","fileExtension":"jpg","mimeType":"image\/jpeg","type":"image"},{"uuid":"c61bcc45-5347-4e98-9990-bc949dad24fa","fileExtension":"mp4","mimeType":"video\/mp4","type":"video"}]}

iOS Nepal
  • 103
  • 1
  • 10

2 Answers2

0

Try with this code,

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
self.client = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];

NSString *tempFileString = [NSTemporaryDirectory() stringByAppendingPathComponent:@"your-app-temp"];
NSURL *filePathtemp = [NSURL fileURLWithPath:tempFileString];

NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://domain/path" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
     [formData appendPartWithFileData:imageData name:@"imageData" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
} error:nil];

[[AFJSONRequestSerializer  serializer] requestWithMultipartFormRequest:request writingStreamContentsToFile:filePathtemp completionHandler:^(NSError *error) {
    NSURLSessionUploadTask *uploadTask = [self.client uploadTaskWithRequest:request fromFile:filePathtemp progress:nil completionHandler:completionHandler];
    [uploadTask resume];
}];
RJV Kumar
  • 2,408
  • 1
  • 19
  • 28
0

you have to upload image separately as a multipart-data using AFNetworing. Like this:-

- (AFHTTPRequestOperation*)POSTAction:(APIRestAction)task
            constructingBodyWithBlock:(void (^)(id<AFMultipartFormData> formData))block
                              success:(void (^)(AFHTTPRequestOperation* operation, id responseObject))success
                              failure:(void (^)(AFHTTPRequestOperation* operation, NSError* error))failure;

Here :- Set you server path in APIRestAction.

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Mandar Belkunde
  • 744
  • 1
  • 5
  • 10