废话不多说,直接上代码!
/**
* 上传单张图片
*/
- (void)uploadPictureWith:(NSString *)URLString
parameters:(id)parameters
data:(NSData *)data
success:(nullable void (^)(ResponseObject * _Nullable responseObject))success
failure:(nullable void (^)(NSError *_Nullable error))failure{
// 1.请求管理器
// AFNetworkReachabilityManager
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
//2.取token(项目中的应用)
NSString *cookiesdata = [[NSUserDefaults standardUserDefaults] valueForKey:@"token"]?[[NSUserDefaults standardUserDefaults] valueForKey:@"token"]:@"";
//3.传token
[sessionManager.requestSerializer setValue:cookiesdata forHTTPHeaderField:@"xx-token"];
NSString *url = @"";
if ([URLString containsString:BaseAPI]) {
url = URLString;
}else{
url = [NSString stringWithFormat:@"%@%@",BaseAPI,URLString];
}
//2.上传文件
[sessionManager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]];
//上传文件参数
[formData appendPartWithFileData:data name:@"image" fileName:fileName mimeType:@"image/jpeg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
//打印上传进度
CGFloat progress = 100.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;
NSLog(@"%f",progress);
[SVProgressHUD showWithStatus:@"正在上传..."];
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
ResponseObject *response = [ResponseObject mj_objectWithKeyValues:responseObject];
// 回调成功之后的block
NSLog(@"%@",response);
success(response);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
//请求成功
NSLog(@"请求成功:%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 回调失败之后的block
[SVProgressHUD showErrorWithStatus:@"上传失败~"];
failure(error);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
//请求失败
NSLog(@"请求失败:%@",error);
}];