iOS高级程序师技术群:622177838,期待你的加入
+ (void)multiPartMoviePost:(NSDictionary *)dicData andImageNames:(NSArray *)images URL:(NSString *)modelurl success:(void (^)(id _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
NSURL *url = [NSURL URLWithString:modelurl];
NSMutableData * bodyData = [NSMutableData data];
for(NSString *key in dicData.allKeys) {
id value = [dicData objectForKey:key];
[bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n",@"POST_BOUNDS"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"%@\r\n",value] dataUsingEncoding:NSUTF8StringEncoding]];
}
for (NSInteger i = 0; i < images.count; i++) {
NSString * contentType;
NSData * videoData;
videoData = [NSData dataWithContentsOfFile:images[i]];
contentType = @"video/mp4";
[bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n",@"POST_BOUNDS"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileArray\"; filename=\"fileArray%ldvideos.mp4\"\r\n",(long)i] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n",contentType] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:videoData];
[bodyData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
// 添加尾部分隔线
[bodyData appendData:[[NSString stringWithFormat:@"--%@--\r\n",@"POST_BOUNDS"] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
[request addValue:[NSString stringWithFormat:@"multipart/form-data;boundary=%@",@"POST_BOUNDS"] forHTTPHeaderField:@"Content-Type"];
[request addValue: [NSString stringWithFormat:@"%zd",bodyData.length] forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:bodyData];
if ([[MLandModel shareInstance] getUserInfo_MLandModel].token != nil) {
[request setValue:[[MLandModel shareInstance] getUserInfo_MLandModel].token forHTTPHeaderField:@"token"];
}
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[manager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
} downloadProgress:^(NSProgress * _Nonnull downloadProgress) {
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
NSLogUTF8(@"%@",responseObject);
success(responseObject);
}] resume];
}
网友评论