ios 多文件上传
/**
* 上传多个文件
*
* @param url 请求接口地址
* @param filedata 文件名称和数据(key:value)
* @param btnName 上传网页的btn (接口)
* @param parms 附加上传参数
*/
-(void)httpSendDataWithUrl:(NSString*)url data:(NSDictionary*)filedata name:(NSString*)btnName parms:(NSDictionary*)parms
{
NSString *httpUrl=url;
//----------------------Post方法的URL和请求头-----------
NSMutableURLRequest *mRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:httpUrl]];
mRequest.HTTPMethod=@"post";
//Content-Type:multipart/form-data; boundary=----WebKitFormBoundarywrkpby4PbAPpMcFc
NSString *str=[NSString stringWithFormat:@"multipart/form-data; boundary=%@",KEYWard];
[mRequest setValue:str forHTTPHeaderField :@"Content-Type"];
mRequest.HTTPBody=[self makeBodydata:filedata name:btnName parms:parms];
//----------------------Post方法的URL和请求头-----------
[NSURLConnection sendAsynchronousRequest:mRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200) {
NSError *error=nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error) {
NSLog(@"json解析错误!!!!!!");
}
else
{
NSLog(@"%@",json);
}
}else{
NSLog(@"服务器内部错误");
}
}else{
NSLog(@"请求错误%@",connectionError);
}
}];
}
-(NSData*)makeBodydata:(NSDictionary*)filedata name:(NSString*)btnName parms:(NSDictionary*)parms
{
NSMutableData *mData = [NSMutableData data];
//准备文件
[filedata 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",KEYWard];
[mStr appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",btnName,key];
[mStr appendString:@"application/octet-stream\r\n"];
[mStr appendString:@"\r\n"];
[mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
//2
[mData appendData:data];
}];
//准备表单中的数据
[parms 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",KEYWard];
[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",KEYWard];
[mData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
return mData.copy;
}
ios 多文件上传的更多相关文章
- 【iOS】文件上传小记
iOS由该系统提供API可以实现可以实现文件的上传和下载,有两种方法来. NSURLConnection与NSURLSession. 当中NSURLConnection是使用非常久的的一种方式.NSU ...
- IOS 多文件上传 Java web端(后台) 使用List<MultipartFile> 接收出现的问题
先上正确的示例: 主要是设置我们的request的content-type为multipart/form-data NSDictionary *param = @{@"assignee&qu ...
- IOS后台文件上传
public ModelAndView GetImage(HttpServletRequest request, HttpServletResponse response) throws Exce ...
- iOS实现文件上传功能模块
iOS实现文件上传功能,首先要知道的是,上传到服务器的数据格式,一般采用HTTP文件上传协议.如下图 如图所示,只要设置好了HTTP的协议格式,就可以实现文件上传功能. 代码如下: //图片上传模块 ...
- iOS开发之网络编程--使用NSURLConnection实现文件上传
前言:使用NSURLConnection实现文件上传有点繁琐. 本文并没有介绍使用第三方框架上传文件. 正文: 这里先提供用于编码测试的接口:http://120.25.226.186:3281 ...
- 多文件上传 iOS功能
多文件上传 iOS功能,原文来自ios教程网整理的,大家可以看看演示:ios.662p.com ,喜欢的朋友可以看看我的博客吧. NSURL* url = [NSURL URLWithString:@ ...
- 实现多文件上传在iOS开发中
该功能实现了实现多文件上传在iOS开发中,喜欢的朋友可以研究一下吧. NSURL* url = [NSURL URLWithString:@"xxx"]; ASIFormDataR ...
- iOS文件上传文件URL错误Invalid parameter not satisfying: fileURL'
一:iOS文件上传提示URL错误 Invalid parameter not satisfying: fileURL' 二:解决方法: NSString *imagePath = [[NSBundle ...
- IOS And WCF 上传文件
IOS And WCF Story 研究IOS上传到WCF图片的小功能,WCF实现服务端的文件上传的例子很多,单独实现IOS发送图片的例子也很多,但是两个结合起来的就很少了. 可以通过base64来上 ...
随机推荐
- WebViewer报错Error loading document: Invalid XOD file: Zip end header data is wrong size!
错误:Error loading document: Invalid XOD file: Zip end header data is wrong size! 解决:https://groups.go ...
- FFmpeg-20160506-snapshot-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 F ...
- IIS7.0配置网站时,提示“ISAPI 和 CGI 限制”
把网站配置到IIS上的时候,访问网站提示如下错误:
- ios cell左滑删除
iOS项目开发小技能 (三) -UITableView实现Cell左划删除等自定义功能 www.MyException.Cn 网友分享于:2015-06-05 浏览:0次 iOS项目开发小技巧 ...
- CSS3的新属性的一下总结
阮一峰:http://www.ruanyifeng.com/blog/2014/02/css_transition_and_animation.html 由于自己经常搞混:animation,tran ...
- LinkIssue: Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or cor
参考:http://blog.csdn.net/junjiehe/article/details/16888197 使用VisualStudio 编译链接中可能出现如下错误: LINK : fatal ...
- cuda 初学大全
转自:http://blog.csdn.net/augusdi/article/details/12529331 cuda 初学大全 1 硬件架构CUDA编程中,习惯称CPU为Host,GPU为Dev ...
- python基础——继承和多态
python基础——继承和多态 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超类 ...
- 指针和引用的区别(c/c++)
http://blog.csdn.net/thisispan/article/details/7456169 ★ 相同点: 1. 都是地址的概念: 指针指向一块内存,它的内容是所指内存的地址:引用 ...
- Mac系统下使用VirtualBox虚拟机安装win7--第二步 创建win7系统
第二步 创建win7系统 启动 Virtual Box 以后,点击窗口左上角的“新建”按钮,如图所示