• 文件上传


    #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:@"上传失败"];
            });
        }];
  • 相关阅读:
    【WPF学习】第六十二章 构建更复杂的模板
    【WPF学习】第六十一章 组织模板资源
    【WPF学习】第六十章 创建控件模板
    【WPF学习】第五十九章 理解控件模板
    Fast-Rcnn论文翻译
    在pytorch下使用tensorboardX(win10;谷歌浏览器;jupyter notebook)
    win10下cuda安装以及利用anaconda安装pytorch-gpu过程
    JS this的指向
    JS promise对象
    JS 正则表达式
  • 原文地址:https://www.cnblogs.com/sunjianfei/p/6566407.html
Copyright © 2020-2023  润新知