AFNetworking 上传文件

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
@第一种
    #pragma mark - AFNetworking上传文件  
    - (void)didClickUploadButtonAction{  
      
        NSString *fileName = @"hmt";  
        //  确定需要上传的文件(假设选择本地的文件)  
        NSURL *filePath = [[NSBundle mainBundle] URLForResource:fileName withExtension:@"png"];  
        NSDictionary *parameters = @{@"name":@"额外的请求参数"};  
        AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];  
        [requestManager POST:@"http://192.168.10.18:8080/AFNetworkingServer/upload" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {  
              
            /** 
             *  appendPartWithFileURL   //  指定上传的文件 
             *  name                    //  指定在服务器中获取对应文件或文本时的key 
             *  fileName                //  指定上传文件的原始文件名 
             *  mimeType                //  指定商家文件的MIME类型 
             */  
            [formData appendPartWithFileURL:filePath name:@"file" fileName:[NSString stringWithFormat:@"%@.png",fileName] mimeType:@"image/png" error:nil];  
              
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {  
              
            [[[UIAlertView alloc] initWithTitle:@"上传结果" message:[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]  delegate:self cancelButtonTitle:@"" otherButtonTitles:nil] show];  
              
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
              
            NSLog(@"获取服务器响应出错");  
              
        }];  
          
    }  

@第二种:
    NSData *imageData = UIImagePNGRepresentation(image);  
      
    //转码成UTF-8  否则可能会出现错误  
      
      
    NSString *URLTmp = [NSString stringWithFormat:@"%@%@",CGBaseURL,path];  
      
    NSString *URLTmps = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
      
    URLTmp = URLTmps;  
      
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:URLTmp parameters:body constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {  
          
        [formData appendPartWithFileData:imageData name:@"headerImg" fileName:@"123.png" mimeType:@"image/png"];  
          
    } error:nil];  
      
      
      
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];  
      
    NSProgress *progress = nil;  
      
      
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];  
      
      
    NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {  
          
        if (error) {  
              
            NSLog(@"Error: %@", error);  
              
        } else {  
              
            block(responseObject);  
        }  
          
    }];  
      
      
    [uploadTask resume];  

标签: idc seo 服务器

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇: JS JAVA 判断是否微信浏览器

下一篇:C#通过ARP获取Mac与IP的对应表