#pragma mark - uploadFile - (void)uploadFileToServerUrl:(NSString *)urlString fromlocalPath:(NSString *)filepath HTTPHeaderField:(NSDictionary *)dict success:(void(^)(NSURLResponse *response, NSData *data))success failure:(void(^)(NSError *error))failure{ NSInputStream *inputFile = [NSInputStream inputStreamWithFileAtPath:filepath]; [self uploadFileToServerUrl:urlString inputStream:inputFile HTTPHeaderField:dict success:success failure:failure]; } - (void)uploadFileToServerUrl:(NSString *)urlString fileData:(NSData *)fileData HTTPHeaderField:(NSDictionary *)dict success:(void(^)(NSURLResponse *response, NSData *data))success failure:(void(^)(NSError *error))failure { NSInputStream *inputFile = [NSInputStream inputStreamWithData:fileData]; [self uploadFileToServerUrl:urlString inputStream:inputFile HTTPHeaderField:dict success:success failure:failure]; } - (void)uploadFileToServerUrl:(NSString *)urlString inputStream:(NSInputStream *)inputStream HTTPHeaderField:(NSDictionary *)dict success:(void(^)(NSURLResponse *response, NSData *data))success failure:(void(^)(NSError *error))failure { NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *mutableRequest = [NSMutableURLRequest new]; mutableRequest = [NSMutableURLRequest requestWithURL:url]; [mutableRequest setHTTPMethod:@"POST"]; [mutableRequest setHTTPBodyStream:inputStream]; for (NSString *key in dict) { [mutableRequest setValue:dict[key] forHTTPHeaderField:key]; } NSURLRequest *request = mutableRequest.copy; NSOperationQueue *queue=[NSOperationQueue mainQueue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { if (connectionError) { failure(connectionError); return ; } else { success(response, data); } }]; }
调用:
NSData *imgData = UIImageJPEGRepresentation(image, 1.0); NSString *lengthStr = [NSString stringWithFormat:@"%lu",(unsigned long)imgData.length]; NSDictionary *httpHeaderField = @{@"Content-Type" : @"image/jpeg", @"Content-Length" : lengthStr}; [self uploadFileToServerUrl:serverURL fileData:imgData HTTPHeaderField:httpHeaderField success:^(NSURLResponse *response, NSData *data) { NSDictionary *dict = [data JSONValue]; if (![dict[@"returnCode"] isEqualToString:@"000000"] || dict == nil) { LYYLog(@"上传失败"); dispatch_async(dispatch_get_main_queue(), ^{ [SVProgressHUD showErrorWithStatus:@"上传失败"]; }); return ; } NSMutableString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding].mutableCopy; LYYLog(@"livenessDetectorFinished%@", str); [SVProgressHUD showSuccessWithStatus:@"上传成功"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSString *js = [NSString stringWithFormat:@"livenessDetectorFinished('%@')",str]; [self evaluateJSWithString:js]; }); } failure:^(NSError *error) { LYYLog (@"%s,error:%@", __FUNCTION__, error.localizedDescription); dispatch_async(dispatch_get_main_queue(), ^{ [SVProgressHUD showErrorWithStatus:@"上传失败"]; }); }];