• iOS开发之多文件上传


     

     

     

     

     

     

     

     

     

     

     

     

     

    //

    //  ViewController.m

    //  B03-多文件上传

    //

    //  Created by 0426iOS on 15/7/1.

    //  Copyright (c) 2015 0426iOS. All rights reserved.

    //

     

    #import "ViewController.h"

     

    @interface ViewController ()

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

    }

     

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

        

        NSString *strUrl = @"http://127.0.0.1/upload/upload-m.php";

        

        //加载两个文件

        NSString *fileName1 = @"oo.png";

        NSString *path1 = [[NSBundle mainBundle] pathForResource:fileName1 ofType:nil];

        NSData *data1 = [NSData dataWithContentsOfFile:path1];

        

        NSString *fileName2 = @"xx.png";

        NSString *path2 = [[NSBundle mainBundle] pathForResource:fileName2 ofType:nil];

        NSData *data2 = [NSData dataWithContentsOfFile:path2];

        NSDictionary *files = @{fileName1:data1,fileName2:data2};

        

        NSDictionary *params = @{@"username":@"hhhhhhhhhhh"};

        

        //上传多个文件

        [self uploadFiles:strUrl files:files name:@"userfile[]" params:params];

        

    }

    #define kBOUNDARY @"aabbcc"

    //上传多个文件

    - (void)uploadFiles:(NSString *)strUrl files:(NSDictionary *)files name:(NSString *)name params:(NSDictionary *)param{

        

        NSURL *url = [NSURL URLWithString:strUrl];

        

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

        request.HTTPMethod = @"post";

        //Content-Type:multipart/form-data; boundary=----WebKitFormBoundarycwuJbGNWchuHGPRT

        NSString *ct = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBOUNDARY];

        [request setValue:ct forHTTPHeaderField:@"Content-Type"];

        

        //

        request.HTTPBody = [self makeBody:files name:name params:param];

        

        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

            //

            if (!connectionError) {

                NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

                if (httpResponse.statusCode == 200) {

                    //

                    id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

                    NSLog(@"%@",json);

                }else{

                    NSLog(@"服务器内部错误");

                }

            }else{

                NSLog(@"请求错误%@",connectionError);

            }

        }];

     

    }

     

     

    //

    - (NSData *)makeBody:(NSDictionary *)files name:(NSString *)name params:(NSDictionary *)param{

        NSMutableData *mData = [NSMutableData data];

        //准备文件

        [files enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSData *data, BOOL *stop) {

    //        ------WebKitFormBoundarycwuJbGNWchuHGPRT

    //        Content-Disposition: form-data; name="userfile[]"; filename="Snip20150701_10.png"

    //        Content-Type: image/png

            //1

            NSMutableString *mStr = [NSMutableString string];

            [mStr appendFormat:@" --%@ ",kBOUNDARY];

            [mStr appendFormat:@"Content-Disposition: form-data; name="%@"; filename="%@" ",name,key];

            [mStr appendString:@"application/octet-stream "];

            [mStr appendString:@" "];

            [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];

            //2

            [mData appendData:data];

            

        }];

        

        

        

        //准备表单中的数据

        [param enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {

    //        ------WebKitFormBoundarycwuJbGNWchuHGPRT

    //        Content-Disposition: form-data; name="username"

    //        

    //        345345

            

            

            NSMutableString *mStr = [NSMutableString string];

            [mStr appendFormat:@" --%@ ",kBOUNDARY];

            [mStr appendFormat:@"Content-Disposition: form-data; name="%@" ",key];

            [mStr appendString:@" "];

            [mStr appendFormat:@"%@ ",value];

            [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];

     

        }];

     

        

        

        

        NSString *end = [NSString stringWithFormat:@"--%@-- ",kBOUNDARY];

        [mData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];

        return mData.copy;

    }

     

     

     

     

     

     

    @end

     

     

     

     

  • 相关阅读:
    git 忽略文件夹权限
    文字特效-shine.js-阴影随动
    微信小程序scroll-view中的坑(因为动态设置高度导致无法下拉)
    gitlab 403 forbidden 报错解决
    Vue子组件调用父组件的方法
    github的小笔记
    windows win10 重装系统 提示不是gpt分区不能安装
    使用html5播放m3u8直播源
    youtube-dl 使用简介
    ABAP RSA 加密
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/5628576.html
Copyright © 2020-2023  润新知