iOS 上传文件
来源:互联网 发布:easymule mac 编辑:程序博客网 时间:2024/06/03 19:24
#define YYEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]-(void) uploadWithUrlStr:(NSString*)url images:(NSDictionary*) images withCardImage:(UIImage*) cardImage parmas:(NSDictionary *)params{ // 文件上传 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; request.HTTPMethod = @"POST"; // 设置请求体 NSMutableData *body = [NSMutableData data]; /***************文件参数***************/ [images enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { // 参数开始的标志 [body appendData:YYEncode(@"--YY\r\n")]; NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", key, key]; [body appendData:YYEncode(disposition)]; NSString *type = [NSString stringWithFormat:@"Content-Type: %@\r\n", @"image/jpeg"]; [body appendData:YYEncode(type)]; [body appendData:YYEncode(@"\r\n")]; [body appendData:obj]; [body appendData:YYEncode(@"\r\n")]; }]; /***************普通参数***************/ [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { // 参数开始的标志 [body appendData:YYEncode(@"--YY\r\n")]; NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key]; [body appendData:YYEncode(disposition)]; [body appendData:YYEncode(@"\r\n")]; [body appendData:YYEncode(obj)]; [body appendData:YYEncode(@"\r\n")]; }]; /***************参数结束***************/ // YY--\r\n [body appendData:YYEncode(@"--YY--\r\n")]; request.HTTPBody = body; // 设置请求头 // 请求体的长度 [request setValue:[NSString stringWithFormat:@"%zd", body.length] forHTTPHeaderField:@"Content-Length"]; // 声明这个POST请求是个文件上传 [request setValue:@"multipart/form-data; boundary=YY" forHTTPHeaderField:@"Content-Type"]; // 发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (data) { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"%@", dict); } else { NSLog(@"上传失败"); } }];}
0 0
- ios文件上传
- 【iOS】文件上传小记
- ios 文件上传
- ios文件上传
- 多文件上传 iOS
- iOS AFNetWorking上传文件
- ios 文件上传
- iOS文件上传注意事项
- iOS 后台上传文件
- IOS文件上传
- iOS文件上传
- IOS文件上传
- iOS 上传文件
- iOS post上传文件
- IOS NSURLConnection 文件上传
- iOS ftp上传文件
- 文件上传 iOS
- ios 文件上传
- 关于Android7.0系统使用webview遇到的一个问题(二级跳转后界面空白)
- Python 引用Flask报错
- 《Java虚拟机原理图解》 1.1、class文件基本组织结构
- 基于文件的视音频自动技审设备
- <div>标签的单选框事件
- iOS 上传文件
- C++中this->的作用
- HttpServletRequest对象的详解
- 关于消息队列的使用----ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ
- c++第五次实验
- Difference between $.extend and $.fn.extend
- Plug-in 对话框
- 类加载的过程(详细)
- 单例模式 (两种)及多线程下的优化