//

//  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开发之多文件上传的更多相关文章

  1. iOS多线程与网络开发之小文件上传

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. /** 取得本地文件的MIMEType */ 2 - (void) getMIMEType { 3 // Socket 实现断点上传 4 5 //apa ...

  2. IOS开发 APP提交程序上传流程

    由于苹果的机制,在非越狱机器上安装应用必须通过官方的App Store,开发者开发好应用后上传App Store,也需要通过审核等环节.AppCan作为一个跨主流平台的一个开发平台,也对ipa包上传A ...

  3. Web开发安全之文件上传安全

    很长一段时间像我这种菜鸡搞一个网站第一时间反应就是找上传,找上传.借此机会把文件上传的安全问题总结一下. 首先看一下DVWA给出的Impossible级别的完整代码: <?php if( iss ...

  4. asp.net(c#)开发中的文件上传组件uploadify的使用方法(带进度条)

    上文件传很常见,现在就文件上传利用HTML的File控件(uploadify)的,这里为大家介绍一下(uploadify)的一些使用方法.在目前Web开发中用的比较多的,可能uploadify(参考h ...

  5. python运维开发(二十一)----文件上传和验证码+session

    内容目录: 文件上传 验证码+session 文件和图片的上传功能 HTML Form表单提交,实例展示 views 代码 HTML ajax提交 原生ajax提交,XMLHttpRequest方式上 ...

  6. Spring boot+Vue全栈开发---Spring Boot文件上传

    https://blog.csdn.net/Day_and_Night_2017/article/details/86980743 文件上传涉及到两个组件:CommonsMultipartResolv ...

  7. 分享一个FileUtil工具类,基本满足web开发中的文件上传,单个文件下载,多个文件下载的需求

    获取该FileUtil工具类具体演示,公众号内回复fileutil20200501即可. package com.example.demo.util; import javax.servlet.htt ...

  8. IOS开发教程之put上传文件的服务器的配置及实例分享-备用

    感谢大神分享 1,HTTP常见的方法 GET 获取指定资源 POST 2M 向指定资源提交数据进行处理请求,在RESTful风格中用于新增资源 HEAD 获取指定资源头部信息PUT 替换指定资源(不支 ...

  9. WPF开发的FTP文件上传工具

    前言 最近楼主在改版的项目里有使用到FTP上传文件,所以看到之前的项目里FTP上传的功能,然后抽个时间学习一番,做着做着就作出了这个工具了(虽然有些验证的功能没加上),但是基本的上传功能还是很好用的, ...

随机推荐

  1. Codeforces 138D World of Darkraft(Multi-Nim)

    [题目链接] http://codeforces.com/problemset/problem/138/D [题目大意] H*W的棋盘中每个点都是L.R.X三者之一,两人轮流选一个点, 若为L则向左下 ...

  2. 【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons

    从任意点出发,贪心染色即可. #include<cstdio> #include<algorithm> using namespace std; int v[200010< ...

  3. 【斜率优化】BZOJ1010 [HNOI2008]玩具装箱toy

    [题目大意] P教授有编号为1...N的N件玩具,第i件玩具长度为Ci.为了方便整理,P教授要求在一个一维容器中的玩具编号是连续的.如果将第i件玩具到第j个玩具放到一 个容器中,那么容器的长度将为 x ...

  4. java 面向接口编程的理解

    初学者可能在学习中会有很多疑惑,为什么要这样,明明可以那样实现,这样做的好处又是什么? 可能会的人觉得很简单很容易理解,甚至可能觉得问的问题很智障,但对于小白来说可能是苦思冥想都不得其解的. 自己身为 ...

  5. Problem C: 零起点学算法18——3个数比较大小

    #include<stdio.h> int main() { int a,b,c; while(scanf("%d %d %d",&a,&b,& ...

  6. 【Python笔记】Python语言基础

    Python是一种解释性(没有编译).交互式.面向对象的语言 1.安装python编译器 版本:Python2.7比较普遍,Python不是向下兼容的软件,因此Python3.x有些东西不好找资料 2 ...

  7. HTML5 Boilerplate笔记(2)(转)

    最近看到了HTML5 Boilerplate模版,系统的学习与了解了一下.在各种CSS库.JS框架层出不穷的今天,能看到这么好的HTML模版,感觉甚爽.写篇博客,推荐给大家使用.   一:HTML5 ...

  8. Linq 简明教程

    一个简单的实例 static void Main(string[] args) { string[] names = { "Alonso", "Zheng", ...

  9. [Android Traffic] Android网络开启、关闭整理

    转载: http://blog.csdn.net/tu_bingbing/article/details/8469871 近段时间由于要对手机网络状况进行判断.开启和关闭,从网上找了些资料,现整理如下 ...

  10. 折腾kubernetes各种问题汇总

    折腾fluend-elasticsearch日志,折腾出一大堆问题,解决这些问题过程中,感觉又了解了不少. 1.如何删除不一致状态下的rc,deployment,service. 在某些情况下,经常发 ...