• 使用AFNetWorking 上传文件/图片


    一 .

    用AFHTTPSessionManager中的这个方法

    - (NSURLSessionDataTask *)POST:(NSString *)URLString
                        parameters:(id)parameters
         constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                          progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress
                           success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                           failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure;
    

    tokenId可以作为参数放到parameters

    然后注意file字段:这个file 就是

    [formData appendPartWithFileData:file[@"buff"] name:@"file" fileName:file[@"name"] mimeType:@"image/*"];
    


    name的值。之前没搞懂。file[@"buff"],file[@"name"] 这个是方法传过来的字典,与这里说的主题没关系。

    详细代码:

    
    

    如果是其他地方如头像,则是不同的key。比如

    [[WLFNetWorkManager shared] POST:FILE_UPLOAD_URL parameters:@{@"tokenId":tokenId} constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            
            [formData appendPartWithFileData:file[@"buff"] name:@"file" fileName:file[@"name"] mimeType:@"image/*"];
            
            
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
            //NSLog(@"uploadProgress = %@",uploadProgress);
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSString *json = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSDictionary *retDict = [[WLFDataTransformTool shareInstance] dictionaryWithJsonString:json];
            complete(retDict);
            /*
             {
                 code = 1;
                 data =     {
                     fileId = 114587;
                     fileSize = 5081851;
                     fileUrl = "/11/2020/3/18/5c4e397d-13e8-4b05-bc18-3a093ca28dc6.jpg";
                 };
                 msg = "文件上传成功";
             }
             */
    
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            faildBlock(error);
        }];
    

    二.  之前项目一直用thrift。现在改成http, 为啥没有用https,后台说他们在后台已经加密了。

    这里也顺便记录一下thrift的使用步骤。库地址:https://github.com/apache/thrift

    后台边写好了thrift文件之后,然后在thrift/ 同级目录下运行命令:

    thrift -out /Users/udc/Desktop/thrift_interface -I ./ --gen cocoa udc_all.thrift
    

    生成

    udc_all.h
    udc_all.m 

    两个OC文件。这两个文件中就是相关接口,直接调用就可以了。另外一些模型啊,返回码,状态码。都在thrift/文件中定义了,不用自己写。

    如果想生成其他语言接口文件,可以试试如下命令。
    #thrift -out /Users/udc/Desktop/thrift_interface -I ./ --gen swift udc_all.thrift
    
    #thrift -out /Users/udc/Desktop/thrift_interface -I ./ --gen cpp udc_all_cpp.thrift
    
    #thrift -out /Users/udc/Desktop/thrift_interface -I ./ --gen js udc_all_js.thrift
    

    用法简略:

    -(void)OC封装的接口请求方法名{
    
        __unsafe_unretained  WLFAPIDataAccess *weakSelf = self;
        [self.asyncQueue addOperationWithBlock:^(void) {
            
            @try {
                
                self.transport = [[TSocketTransport alloc] initWithHostname:SERVER_HOST_IP port:SERVER_PORT];//用主机+IP初始化
                self.protocol = [[TBinaryProtocol alloc] initWithTransport:self.transport];
                self.server = [[UdcApiServiceClient alloc]initWithProtocol:self.protocol];
                
                NSError *error;
                FileInfoResp *fileresponseInfo = [self.server uploadFile:self.udchead file:fileData error:&error]; //调用上面生成udc_all.h文件中的接口 得到返回的数据模型 回调出去
                
                //DLog(@"UploadFile ret = %@",fileresponseInfo);
                // UploadFile ret = FileInfoResp(response:ResponseInfo(code:1,message:"文件上传成功。"),fileInfo:FileInfo(fileId:24709,fileUrl:"/11/2018/11/8/8ad9212c-32ad-4d9f-8147-3c48077a21b3.JPG",fileName:"IMG_0005",fileSize:473044,originalFileName:"(null)",pkId:0))
    
    
                [self.transport close];//关闭thrift传输 避免浪费资源
                
                [self asyncRequestResultWith:fileresponseInfo complete:complete failed:faildBlock];
                
            }
            @catch (NSException *e) {
                NSString *errorMsg = e.description;
                //DLog(@" UploadFile Error %@", errorMsg);
                [weakSelf.mainQueue addOperationWithBlock:^(void) {
                    complete(errorMsg);
                }];
            }
            
        }];
    
    
    }
    
    此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935. 我的gitHub: (学习代码都在gitHub) https://github.com/nwgdegitHub/
  • 相关阅读:
    DAO模式多表联查
    使用ADO.NET访问数据库
    连接查询和分组查询
    模糊查询和聚合函数
    poj 1220 NUMBER BASE CONVERSION
    poj 1964 City Game
    Odd number problem
    POJ 2983 M × N Puzzle
    L O V E
    【Mybatis】【3】处理大于号小于号及其他特殊字符
  • 原文地址:https://www.cnblogs.com/liuw-flexi/p/12517346.html
Copyright © 2020-2023  润新知