Use AFNetWorking upload files / images

A. Before the project has been used thrift. Now replaced http, why not use https, the background in the background that they have encrypted.

 

This method is used in 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 as parameters into parameters

Then pay attention to file field: This file is

 

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

The value of name. Before I did not get to know.

 

[[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);
    }];

 

II. There is also the way to record it using the steps of thrift.

After back side written thrift file, and then run the command in thrift / sibling directory:

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

Form

udc_all.h
udc_all.m 

two OC files. These two documents is related to interface directly call it. Other models ah, return code, status code. Are defined in thrift / file, do not write themselves.

If you want to generate other language interface files, you can try the following commands.

#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

 

Use simple:

 

- (void) -OC encapsulated interface request method name { 

    __unsafe_unretained WLFAPIDataAccess * weakSelf = Self; 
    [self.asyncQueue addOperationWithBlock: ^ ( void ) { 
        
        @try { 
            
            self.transport = [[TSocketTransport the alloc] initWithHostname: SERVER_HOST_IP Port: SERVER_PORT]; // + IP host computer initializes 
            self.protocol = [[TBinaryProtocol the alloc] initWithTransport: self.transport]; 
            self.server = [[UdcApiServiceClient the alloc] initWithProtocol: self.protocol]; 
            
            the NSError * error; 
            FileInfoRespfileresponseInfo = * [self.server the uploadFile: self.udchead File: fileData error: & error]; // call generated above udc_all.h file interface callback model returns data obtained out 
            
            // dlog (@ "UploadFile% RET = @ ", fileresponseInfo);
             // UploadFile RET = FileInfoResp (the Response: ResponseInfo (code: 1, the Message:." file uploaded successfully "), 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]; // Close thrift transmission avoidance waste of resources [self asyncRequestResultWith: fileresponseInfo complete: complete failed: faildBlock]; } @catch (NSException *e) { NSString *errorMsg = e.description; //DLog(@" UploadFile Error %@", errorMsg); [weakSelf.mainQueue addOperationWithBlock:^(void) { complete(errorMsg); }]; } }]; }

 

Guess you like

Origin www.cnblogs.com/liuw-flexi/p/12517346.html