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 ...
随机推荐
- Android事件分发机制(一) Touch 事件的分发和消费机制
Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev).onInterceptTouchEvent(MotionEvent ev). ...
- C#编写的通过汉字得到拼音和五笔码
public static class SpellAndWbConfig { #region 变量声明 // XML文件读取实例 /// <summary> /// XML文件读取实例 / ...
- 9,SFDC 管理员篇 - 安全设置
1, 使用Profile控制权限 (整体层面) Setup | Manage Users | Profiles 总结下,一个用户只能有一个Profile,但是可以有多个Permission S ...
- WEB-INF下jsp跳转
今天才知道:浏览器是不允许直接访问WEB-INF文件夹的 瞬间感觉自己好shi的有没有,纠结了2天 看来还得通过springMVC来跳,,,
- linux上安装配置vsftpd
启动: /usr/sbin/vsftpd & 端口占用: lsof -i:21 位置: whereis vsftpd 默认配置下,匿名用户登录 vsftpd 服务后的根目录是 /var/ftp ...
- iOS nib file owner
nib文件中的file owner属性,设定后app在运行时加载nib文件的过程中会通过file owner重新建立nib文件中描述的控件与其在file owner中对应的IBOutlet或IBAct ...
- Jenkins+Maven+Sonar系统持续集成环境部署以及配置
一.Jenkins介绍以及安装 什么是持续集成? 随着软件开发复杂度的不断提高,团队开发成员间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题.尤其是近些年来,敏捷(Ag ...
- css绘制特殊图形基础
1.等腰三角形 .isosceles{ width:; height:; border:30px solid; border-left-color: transparent; border-right ...
- 表格制作模块xlwt
import xlwtworkbook = xlwt.Workbook(encoding = 'ascii') #创建workbook 括号内容视情况而定sheetname = 'Sheet'book ...
- 用定时器令P0(或其它IO口)产生多路方波
void Timer0_isr(void) interrupt 1 using 1{ static unsigned char i; //重新赋值 12M晶振计算,指令周期1uS,500x2=1mS ...