zoukankan      html  css  js  c++  java
  • 源码0603-08-掌握-NSURLSession-上传

    //  ViewController.m
    //  08-掌握-NSURLSession-上传#define XMGBoundary @"520it"
    #define XMGEncode(string) [string dataUsingEncoding:NSUTF8StringEncoding]
    #define XMGNewLine [@"
    " dataUsingEncoding:NSUTF8StringEncoding]
    
    #import "ViewController.h"
    
    @interface ViewController () 
    /** session */
    @property (nonatomic, strong) NSURLSession *session;
    @end
    
    @implementation ViewController
    
    - (NSURLSession *)session
    {
        if (!_session) {
            NSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];
            cfg.timeoutIntervalForRequest = 10;
            // 是否允许使用蜂窝网络(手机自带网络)
            cfg.allowsCellularAccess = YES;
            _session = [NSURLSession sessionWithConfiguration:cfg];
        }
        return _session;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/upload"]];
        request.HTTPMethod = @"POST";
        
        // 设置请求头(告诉服务器,这是一个文件上传的请求)
        [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", XMGBoundary] forHTTPHeaderField:@"Content-Type"];
        
        // 设置请求体
        NSMutableData *body = [NSMutableData data];
        
        // 文件参数
        // 分割线
        [body appendData:XMGEncode(@"--")];
        [body appendData:XMGEncode(XMGBoundary)];
        [body appendData:XMGNewLine];
        
        // 文件参数名
        [body appendData:XMGEncode([NSString stringWithFormat:@"Content-Disposition: form-data; name="file"; filename="test.png""])];
        [body appendData:XMGNewLine];
        
        // 文件的类型
        [body appendData:XMGEncode([NSString stringWithFormat:@"Content-Type: image/png"])];
        [body appendData:XMGNewLine];
        
        // 文件数据
        [body appendData:XMGNewLine];
        [body appendData:[NSData dataWithContentsOfFile:@"/Users/xiaomage/Desktop/test.png"]];
        [body appendData:XMGNewLine];
        
        // 结束标记
        /*
         --分割线--
    
         */
        [body appendData:XMGEncode(@"--")];
        [body appendData:XMGEncode(XMGBoundary)];
        [body appendData:XMGEncode(@"--")];
        [body appendData:XMGNewLine];
        
        [[self.session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            NSLog(@"-------%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
        }] resume];
    }
    
    @end
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    1438.最小公倍数
    1441.人见人爱A^B
    1083.特殊乘法
    1153.括号匹配
    1089.数字翻转
    1042.coincidence(动态规划求最长公共子序列)
    图的m着色问题pascal程序
    最佳调度问题pascal程序
    试卷批分打表程序
    迷宫问题pascal程序
  • 原文地址:https://www.cnblogs.com/laugh/p/6611708.html
Copyright © 2011-2022 走看看