/// /// 上传图片 + (AFHTTPRequestOperation *)uploadImageWithUrl:(NSString *)url image:(UIImage *)image completion:(HYBRequestCompletion)completion errorBlock:(HYBErrorBlock)errorBlock { url = [url addPrefix:@"/CosmetologyShop"]; if ([HYBUserInfoTool userToken]) { url = [NSString stringWithFormat:@"%@?TOKEN=%@", url, [HYBUserInfoTool userToken]]; } AFHTTPRequestOperationManager *manager = [self operationManagerWithBaseUrl:kFileBaseUrl]; AFHTTPRequestOperation *op = [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { NSData *imageData = UIImageJPEGRepresentation(image, 1); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *str = [formatter stringFromDate:[NSDate date]]; NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str]; // 上传图片,以文件流的格式 [formData appendPartWithFileData:imageData name:@"myfiles" fileName:fileName mimeType:@"image/jpeg"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { completion(responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { errorBlock(error); }]; return op; }
注意这里上传的是文件流,具体参数说明,看AFN的API说明。
name这里是服务器中指定的文件夹名。