iOS网络 POST模拟表单上传单个与多个文件(直接调用分类里的方法即可)
2015-02-17 16:17
691 查看
// // ViewController.m // 01-POST上传文件 // // Created by 刘凡 on 15/2/8. // Copyright (c) 2015年 itcast. All rights reserved. // #import "ViewController.h" #import "NSMutableURLRequest+Multipart.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 { [self multiPostUpload]; } // MARK: - 多个文件上传 - (void)multiPostUpload { NSURL *url = [NSURL URLWithString:@"http://localhost/post/upload-m.php"]; NSURL *fileUrl1 = [[NSBundle mainBundle] URLForResource:@"demo.jpg" withExtension:nil]; NSData *data1 = [NSData dataWithContentsOfURL:fileUrl1]; NSURL *fileURL2 = [[NSBundle mainBundle] URLForResource:@"001.png" withExtension:nil]; NSData *data2 = [NSData dataWithContentsOfURL:fileURL2]; NSArray *dataArray = @[data1, data2]; NSArray *fileNames = @[@"demo.jpg", @"001.png"]; // NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url keyName:@"userfile" fileDataArray:dataArray fileNames:fileNames]; // NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url keyName:@"userfile" fileURLs:@[fileUrl1, fileURL2] fileNames:fileNames]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url keyName:@"userfile" fileURLs:@[fileUrl1, fileURL2]]; // 3. connection [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]); }]; } // MARK: - 单个文件上传 - (void)postUpload { // 1. url NSURL *url = [NSURL URLWithString:@"http://localhost/post/upload.php"]; // 2. request NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"demo.jpg" withExtension:nil]; NSData *data = [NSData dataWithContentsOfURL:fileUrl]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url keyName:@"userfile" fileData:data fileName:@"bcd.jpg"]; // 3. connection [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]); }]; } @end
http://pan.baidu.com/s/1ntqTaWT
相关文章推荐
- iOS网络 POST原始的模拟表单上传
- HTTPCLIENT POST 模拟有上传文件和文本字段的动态表单
- HttpClien实现使用post方式模拟表单上传大文件和字符参数
- HttpClient4.5.2模拟表单post提交上传文件
- HttpClient PostMethod模拟带文件上传+普通字段的http请求(可解决文件为网络文件的问题)
- SpringMVC 使用uploadify插件上传文件,post方法无法直接获取参数问题
- IOS学习 网络 NSURLSession和POST文件上传
- 使用python模拟post表单上传文件
- Android学习笔记---28_网络通信之通过HTTP协议实现文件上传,组拼http 的post方法,传输内容
- PHP使用curl模拟post上传及接收文件的方法
- iOS AFN 封装POST网络请求(AFURLSessionManager) (续一) upload表单提交方法
- Servlet实现文件上传到服务器(不用表单,直接用代码模拟表单提交)
- java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例
- PHP使用curl模拟post上传及接收文件的方法
- CURL模拟表单post提交及相关常用参数的使用(包括提交表单同时上传文件)
- Qt HTTP网络相关GET,POST(HTTP 模拟POST 表单(multipartform)最简单和正式的方法)
- 三种方法实现php网络请求(表单操作文件上传)
- js_ajax模拟form表单提交_多文件上传_支持单个删除
- ios 对于AFNetworking3.0之前版本的Get、Post请求的封装(直接调用即可)