美文网首页
iOS AFNetworking实现表单(multipart)形

iOS AFNetworking实现表单(multipart)形

作者: wzpforwork | 来源:发表于2017-05-22 14:09 被阅读0次

    //pragma mark -- 上传图片 -- +(NSURLSessionDataTask *)uploadWithURLString:(NSString *)URLString parameters:(id)parameters progress:(RequestProgress)progress uploadParam:(NSString *)imagePath success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure { AFHTTPSessionManager *manager = [self sharedHTTPSessionManager]; URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURLSessionDataTask *session = [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { //这步是关键 file:图片保存地址 NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"file:%@",imagePath]]; [formData appendPartWithFileURL:imageUrl name:@"photo" error:nil]; } progress:^(NSProgress * _Nonnull uploadProgress){ if(progress){ progress(uploadProgress); } } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (failure) { failure(error); } }]; return session; }

    相关文章

      网友评论

          本文标题:iOS AFNetworking实现表单(multipart)形

          本文链接:https://www.haomeiwen.com/subject/ycgbxxtx.html