-(void)uploadDataToServer{
//字符串乱序
NSString *fineNameString = [[NSString alloc]init];
for (int i = 0; i < 32; i++) {
int number = arc4random() % 36;
if (number < 10) {
int figure = arc4random() % 10;
NSString *tempString = [NSString stringWithFormat:@"%d", figure];
fineNameString = [fineNameString stringByAppendingString:tempString];
}else {
int figure = (arc4random() % 26) + 97;
char character = figure;
NSString *tempString = [NSString stringWithFormat:@"%c", character];
fineNameString = [fineNameString stringByAppendingString:tempString];
}
}
//通过AFN上传数据到服务器
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"image/jpeg", @"image/png", @"application/octet-stream", @"text/json", @"video/mov", nil];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//服务器地址
NSString *uploadAddress = @"http://188.188.188.188:10898/video";
[manager POST:uploadAddress parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/xxx.mov"];
NSData *videoData = [NSData dataWithContentsOfFile:path];
NSString *fileName = [NSString stringWithFormat:@"%@.mov",fineNameString];
//formData的其他append方法上传文件失败
[formData appendPartWithFileData:videoData name:@"xxx.mov" fileName:fileName mimeType:@"video/mov"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"视频上传成功 %@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"视频上传失败 %@", error);
}];
}