0
我使用AFMultipartFormData
發送多張圖片到服務器使用一個for循環在時間獲取一幅圖像:運行的方法時AFMultipartFormData完成上傳
NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
NSError *error;
NSString *mystring = @"noName";
//FOR Loop Start
for(NSData *eachImage in dataStringArray) {
NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:eachImage name:@"myImage"];
[formData appendPartWithFormData:[mystring dataUsingEncoding:NSUTF8StringEncoding]
name:@"FileName"];
} error:&error];
NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
return;
}
[uploadedImageIDs addObject:[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]];
}];
[task resume];
}//for end
[self makeJSON:uploadedImageIDs];
但不知何故,它之前調用makeJSON
方法。由於有多張圖片,因此我需要在所有圖片的上傳完成後調用。
只需在塊內調用[self makeJSON:uploadedImageIDs]; –