一个上传文件报错的问题,问题已经验证。
报错信息:
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: request too large (413)" UserInfo={NSLocalizedDescription=Request failed: request too large (413), NSErrorFailingURLKey=https:/***.com/demand/upload/qiNiuYun, com.alamofire.serialization.response.error.data={length = 183, bytes = 0x3c68746d 6c3e0d0a 3c686561 643e3c74 ... 2f68746d 6c3e0d0a }, com.alamofire.serialization.response.error.response=
Connection = (
close
);
"Content-Length" = (
183
);
"Content-Type" = (
"text/html"
);
Date = (
"Thu, 29 Apr 2021 07:46:19 GMT"
);
Server = (
"nginx/1.18.0"
);
} }}
解决方法:
Nginx默认的request body为1M,我们上传文件大于这个值就会爆413问题
找后台吧。调整下Nginx配置文件,反正锅不在移动端。让他们改去
另外添加一个接口上传视频文件的简单封装:
/**
* 上传视频
*
* @param urlString 请求的url
* @param vedioUrl 选择视频后,视频地址
* @param fileName 文件名(上传后文件夹名,后端定义)
* @param parameters 参数
* @param progressValue 数据请求进度
* @param success 请求成功的返回
* @param faild 请求失败的返回
*/
- (AFHTTPSessionManager *)uploadFileWithUrl:(nonnull NSString *)urlString
withVedioURL:(NSURL*)vedioUrl
fileName:(NSString*)fileName
parameters:(nullableNSDictionary*)parameters
progress:(void(^__nullable)(NSProgress*_NonnulluploadProgress))progressValue
success:(void(^__nullable)(id_NullableresponseObject))success
failure:(void(^__nullable)(id_NullableerrorObject))faild{
// 初始化Manager
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[manager.requestSerializer setValue:@"form/data" forHTTPHeaderField:@"Content-Type"];
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
@"text/html",
@"text/json",
@"text/javascript",
@"text/plain",
@"multipart/form-data",
nil];
manager.responseSerializer= responseSerializer;
//设置超时时间
manager.requestSerializer.timeoutInterval = 15.0;
NSLog(@"请求URL:\n %@ \n 请求参数:\n=== %@ =====",urlString,parameters);
[managerPOST:urlString parameters:parameters constructingBodyWithBlock:^(id
NSString *fileUrl = [[vedioUrl absoluteString] stringByRemovingPercentEncoding];
NSString *extension = [fileUrl componentsSeparatedByString:@"."].lastObject;
//文件类型
NSString*mimeType = [NSStringstringWithFormat:@"video/%@", extension];
//以当前时间,重新命名文件名(或者自定义就行,不影响上传是否成功)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat=@"yyyyMMddHHmmss";
NSString*str = [formatterstringFromDate:[NSDate date]];
NSString*fileName = [NSStringstringWithFormat:@"%@.%@", str,extension];
[formData appendPartWithFileURL:vedioUrlname:fileNamefileName:fileNamemimeType:mimeTypeerror:nil];
}progress:^(NSProgress*_NonnulluploadProgress) {
if(progressValue) {
progressValue(uploadProgress);
}
}success:^(NSURLSessionDataTask*_Nonnulltask,id _NullableresponseObject) {
//请求成功,解析数据
NSLog(@"请求结果:\n====== %@ =======\n",responseObject);
id result = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
if([result[@"code"]integerValue] ==200) {
if(success) {
success(result);
}
} else{
GhostShow(result[@"msg"]);
}
}failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
returnmanager;
}