• 源码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
  • 相关阅读:
    机器学习第一练(铁达尼号罹难者预测)
    Codewars题记 :Find the missing letter
    Codewars题记 :Take a Ten Minute Walk
    Codewars题记 :Some numbers have funny properties.
    Codewars题记 :Count the number of Duplicates
    Java图片合成工具类
    解决Libreoffice在Linux服务器上,重启Tomcat但是Libreoffice8100端口还一直占用的问题
    Java对Linux进程关闭
    Java多张图片合成PDF
    java下载文件到本地磁盘
  • 原文地址:https://www.cnblogs.com/laugh/p/6611708.html
Copyright © 2020-2023  润新知