iOS開發之多檔案上傳

來源:互聯網
上載者:User

標籤:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

//

//  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:@"\r\n--%@\r\n",kBOUNDARY];

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

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

        [mStr appendString:@"\r\n"];

        [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:@"\r\n--%@\r\n",kBOUNDARY];

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

        [mStr appendString:@"\r\n"];

        [mStr appendFormat:@"%@\r\n",value];

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

 

    }];

 

    

    

    

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

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

    return mData.copy;

}

 

 

 

 

 

 

@end

 

 

 

 

iOS開發之多檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.