文件夹下面的图片压缩上传服务器-----之代码封装
来源:互联网 发布:怎样做淘宝模板 编辑:程序博客网 时间:2024/05/17 09:06
首先导入头文件以及宏定义
#import "SSZipArchive.h"
#define HMFileBoundary @"heima"
#define HMNewLine @"\r\n"
#define HMEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]
//封装 获取一个文件的 MIMEType
- (NSString *)MIMEType:(NSURL *)url{//1.创建一个请求
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//2.发送请求(返回响应)
NSURLResponse *response=nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
//获得MIMEType
return response.MIMEType;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSString *caches=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
//0.获得需要压缩的文件
NSString *images=[caches stringByAppendingPathComponent:@"images"];
//1.创建一个zip文件
NSString *zipFile=[caches stringByAppendingPathComponent:@"images.zip"];
BOOL result=[SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:images];
if (result) {
NSString *MIMEType=[self MIMEType:[NSURL URLWithString:zipFile]];
NSData *data=[NSData dataWithContentsOfFile:zipFile];
[self upload:@"images.zip" mimeType:MIMEType fileData:data params:nil];
}
}
//封装好了 下面是上传的文件参数和非文件参数的拼接
//1.请求路径(上传文件)
NSURL *url=[NSURL URLWithString:@"http://192.168.1.121:8090/upload"];
//2.创建一个POST请求
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
request.HTTPMethod=@"POST";
//3.设置请求体积
NSMutableData *body=[NSMutableData data];
//3.1文件参数
[body appendData:HMEncode(@"--")];
[body appendData:HMEncode(HMFileBoundary)];
[body appendData:HMEncode(HMNewLine)];
// string.Format("Content-Disposition:form-data;name=\"uploadedfile\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName);
NSString *Disposition=[NSString stringWithFormat:@"Content-Disposition:form-data;name=\"uploadedfile\"; filename=\"%@\"",filename];
[body appendData:HMEncode(Disposition)];
[body appendData:HMEncode(HMNewLine)];
//[body appendData:HMEncode([NSString stringWithFormat:@"Content-Type: %@", mimeType])];
[body appendData:HMEncode(@"Content-Type: application/octet-stream")];
[body appendData:HMEncode(HMNewLine)];
[body appendData:HMEncode(HMNewLine)];
[body appendData:fileData];
[body appendData:HMEncode(HMNewLine)];
//3.2非文件参数
[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[body appendData:HMEncode(@"--")];
[body appendData:HMEncode(HMFileBoundary)];
[body appendData:HMEncode(HMNewLine)];
NSString *disposition=[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"",key];
[body appendData:HMEncode(disposition)];
[body appendData:HMEncode(HMNewLine)];
[body appendData:HMEncode(HMNewLine)];
[body appendData:HMEncode([obj description])];
[body appendData:HMEncode(HMNewLine)];
}];
//3.3结束标记
[body appendData:HMEncode(@"--")];
[body appendData:HMEncode(HMFileBoundary)];
[body appendData:HMEncode(@"--")];
[body appendData:HMEncode(HMNewLine)];
request.HTTPBody = body;
//4设置请求头(告诉服务器这次传得是文件数据 现在发送的是一个文件上传请求)
NSString *contentType=[NSString stringWithFormat:@"multipart/form-data; boundary=%@", HMFileBoundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSString *contentLenth=[NSString stringWithFormat:@"%ld",body.length];
[request setValue:contentLenth forHTTPHeaderField:@"Content-Length"];
//5.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@",dict);
}];
}
0 0
- 文件夹下面的图片压缩上传服务器-----之代码封装
- 公共代码之图片上传压缩处理
- 图片上传之压缩
- 图片上传之压缩
- 图片上传之压缩
- 压缩文件夹下面的所有文件和文件夹
- iOS 图片上传之压缩
- ASP.NET(C#)图片上传压缩成缩略图的代码
- 相册压缩图片,上传服务器方法
- IOS图片压缩上传服务器终极解决方案
- 图片压缩,Base64编码后上传服务器
- 压缩本地图片并上传至服务器
- Android图片压缩并上传到服务器
- IOS学习之ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- 选择文件夹代码的封装
- summernote图片上传功能保存到服务器指定文件夹+php代码+java方法
- 向ftp服务器上传文件夹代码
- PHP之图片上传到服务器、上传的错误类型
- html的块级元素和内联元素
- [leetcode-275]H-Index II(java)
- CSS圆角DIV
- Properties(配置文件类)
- 再谈高级程序员核心竞争力
- 文件夹下面的图片压缩上传服务器-----之代码封装
- 【软件使用】解决Firefox在ubuntu和Windows之间同步问题
- poj3255基础次短路
- Windows 多进程通信API总结
- AWK 简明教程
- python中__name__=='__main__'的作用
- Mac SVN 命令行
- 对数组的两个子有序段进行合并
- Windows多线程问题