iOS Multipart上传单张图片
- (void)uploadFile:(NSDictionary *)dic {
NSURL *URL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@addVisitRecord",SERVER_ADDRESS]];
request = [[NSMutableURLRequest alloc] initWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
NSString *boundary = @"wfWiEWrgEFA9A78512weF7106A";
request.HTTPMethod = @"POST";
request.allHTTPHeaderFields = @{@"Content-Type":[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]};
//multipart/form-data格式按照构建上传数据
NSMutableData *postData = [[NSMutableData alloc] init];
for (NSString *key in dic) {
NSString *pair = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n",boundary,key];
[postData appendData:[pair dataUsingEncoding:NSUTF8StringEncoding]];
id value = [dic objectForKey:key];
if ([value isKindOfClass:[NSString class]]) {
[postData appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([value isKindOfClass:[NSData class]]){
[postData appendData:value];
}
[postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
//文件部分
NSString *filename = [filePath lastPathComponent];
NSString *contentType = AFContentTypeForPathExtension([filePath pathExtension]);
NSString *filePair = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"; filename=\"%@\";Content-Type=%@\r\n\r\n",boundary,@"img",filename,contentType];
[postData appendData:[filePair dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:self.imageSecondFile]; //加入文件的数据
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
request.HTTPBody = postData;
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)postData.length] forHTTPHeaderField:@"Content-Length"];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[_connection start];
}
#pragma mark - connection delegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"reveive Response:\n%@",response);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if (!_reveivedData) {
_reveivedData = [[NSMutableData alloc]init];
}
[_reveivedData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
[[CRMDatahandle shareDataHandle] saveLocalData:@"REFRESH_PLAY" value:@"YES"];
NSError *err;
NSDictionary *note = [NSJSONSerialization JSONObjectWithData:_reveivedData options:NSJSONReadingMutableContainers error:&err];
if ([note[@"success"] integerValue] == 1) {
[CRMDatahandle hudWithText:@"添加成功!" atView:self.view];
[[IQKeyboardManager sharedManager] setEnable:NO];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(succesPaly) userInfo:nil repeats:NO];
}
iOS Multipart上传单张图片的更多相关文章
- Uploadify在MVC中使用方法案例(一个视图多次上传单张图片)
Controller 中代码和 上一节文章(http://www.cnblogs.com/yechangzhong-826217795/p/3785842.html )一样 视图中代码如下: < ...
- Uploadify在MVC中使用方法案例(上传单张图片)
在View视图中: <link href="/Scripts/uploadify-v3.2.1/uploadify.css" rel="stylesheet&quo ...
- 封装组件el-upload通过v-model (一): 上传单张图片
ElementUI 中的el-upload 上传图片 我进行了二次封装.(默认大家都是有一定的vue基础的,细节就不过多的讲了) 在项目中我们主要拿到图片或者其他的一些参数 ,我这里是上传后返回的Gu ...
- 微信小程序云开发-云存储-上传单张照片到云存储并显示到页面上
一.wxml文件 页面上写上传图片的按钮,按钮绑定chooseImg. <button bindtap="chooseImg" type="primary" ...
- struts2学习笔记--上传单个和批量文件示例
struts2提供了对上传文件的支持,将上传后的文件封装为java.io.File对象,开发者只需要在Action中定义一个File类型的变量,然后直接使用该变量,将它复制到目的目录即可. 单个文件上 ...
- 微信JSSDK多图片上传并且解决IOS系统上传一直加载的问题
微信多图片上传必须挨个上传,也就是不能并行,得串行: 那么我们可以定义一个如下所示的上传函数: var serverIds = []; function uploadImages(localImage ...
- java接受安卓及ios App上传的图片,并保存到阿里OSS
做后台的时候,写了两个方法,分别用来获取安卓和苹果IOS端上传的头像,保存到阿里云OSS图片存储服务器上.(SMM框架) 安卓及H5版本: /** * 上传用户头像 */ @RequestMappin ...
- HTML实现点击时的阴影(:active)(已解决iOS微信上无法使用)
一般是用这个委类:active 参考:http://www.w3school.com.cn/cssref/selector_active.asp 但是业界上实现的效果中,今日头条的手机端网页却不是这样 ...
- 如何在iOS地图上高效的显示大量数据
2016-01-13 / 23:02:13 刚才在微信上看到这篇由cocoachina翻译小组成员翻译的文章,觉得还是挺值得参考的,因此转载至此,原文请移步:http://robots.thought ...
随机推荐
- 智能指针 ADO数据库连接
ADO库包含三个基本接口:_ConnectionPtr接口._CommandPtr接口和_RecordsetPtr接口._ConnectionPtr接口返回一个记录集或一个空指针.通常使用它来创建一个 ...
- python os.system()返回值判断
最近遇到os.system()执行系统命令的情况,上网搜集了一下资料,整理如下,以备不时之需,同时也希望能帮到某些人. 一.python中的 os.system(cmd)的返回值与linux命令返回值 ...
- VBS数组
定义一个数组: dim a(3).这里要注意在VBS里面数组不像其他的例如C,C#,JAVA等数组用[]作为数组标志.VBS采用的是().还需要注意的是,这里定义的数组包含a(0),a(1),a(2) ...
- 关于ILDASM.EXE的知识整理
因为现在用的VS2010,发现,这个工具自己就带着ILDASM.EXE这个反编译工具 具体的查找方式为: C:\Program Files\Microsoft SDKS\Windows\V7.0\bi ...
- js实现图片的淡入淡出
思想: 其实是运动的一种,就是当鼠标移入div中时,将div的透明度变大, 当鼠标移动出来的时候透明度变回原来. 你可以尝试写一下,不会再看看代码 <style> #div1{ width ...
- MVC4.0中项目发布遇到IE11时session存入URL中,导致记不住密码的问题
///MVC4.0中项目发布遇到IE11时session存入URL中,导致记不住密码的问题,在webconfig中配置<system.web><authentication mode ...
- 各种数据库分页sql
1.oracle数据库分页 select * from (select a.*,rownum rc from 表名 where rownum<=endrow) a where a.rc>= ...
- 使用SftpDrive+SourceInsight阅读开源代码
在虚拟机环境下使用Linux编写和阅读代码,我之前一直是通过Xshell利用ssh登录至虚拟机从而在命令行下使用vim来进行的.目前有阅读开源代码的需要,虽然vim+ctags+cscope可以完成这 ...
- rotate the clock
A program test: You are given N round clocks. Every clock has M hands, and these hands can point to ...
- C# 命令绑定
在构建WinForm程序中,为了使系统的结构清晰,有好的用户交互体验,实现不同按钮之间的交互,不使主窗体里面的代码臃肿.将按钮的命令通过类进行绑定,实现命令的管理使很有必要的. 该文章是将如何实现Bu ...