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"}]}
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];
}];
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.