zoukankan      html  css  js  c++  java
  • 文件上传

    #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:@"上传失败"];
            });
        }];
  • 相关阅读:
    如何让一个浮动垂直居中:两种方式!带来效果~~~~~~
    rgba()和opacity之间的区别(面试题)
    常用浏览器内核!IE,Chrome ,Firefox,Safari,Opera 等内核
    有关Option.inSamplSize 和 Compress 图片压缩
    Android App 启动 Activity 创建解析
     (转)windows一台电脑添加多个git账号
    Handler向子线程发送数据
    Android Touch事件分发
    int 转十六进制
    JVM client模式和Server模式
  • 原文地址:https://www.cnblogs.com/sunjianfei/p/6566407.html
Copyright © 2011-2022 走看看