IOS 上传下载
下载地址:https://github.com/samsoffes/ssziparchive
注意:需要引入libz.dylib框架
// Unzipping
NSString *zipPath = @"path_to_your_zip_file";
NSString *destinationPath = @"path_to_the_folder_where_you_want_it_unzipped";
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
// Zipping
NSString *zippedPath = @"path_where_you_want_the_file_created";
NSArray *inputPaths = [NSArray arrayWithObjects:
[[NSBundle mainBundle] pathForResource:@"photo1" ofType:@"jpg"],
[[NSBundle mainBundle] pathForResource:@"photo2" ofType:@"jpg"]
nil];
[SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths];
Content-Type multipart/form-data; boundary=本次上传标示字符串(不能中文)
--本次上传标示字符串
Content-Disposition: form-data; name="服务端字段"; filename="上传文件名"
Content-Type: 上传文件MIMEType
要上传的二进制数据
--本次上传标示字符串
Content-Disposition: form-data; name="submit"
Submit
--本次上传标示字符串--
类型 |
文件拓展名 |
MIMEType |
图片 |
png |
image/png |
bmpdib |
image/bmp |
|
jpejpegjpg |
image/jpeg |
|
gif |
image/gif |
|
多媒体 |
mp3 |
audio/mpeg |
mp4mpg4m4vmp4v |
video/mp4 |
|
文本 |
js |
application/javascript |
|
application/pdf |
|
text xt |
text/plain |
|
json |
application/json |
|
xml |
text/xml |
post上传
// // UploadFile.m // 02.Post上传 #import "UploadFile.h" @implementation UploadFile // 拼接字符串 static NSString *boundaryStr = @"--"; // 分隔字符串 static NSString *randomIDStr; // 本次上传标示字符串 static NSString *uploadID; // 上传(php)脚本中,接收文件字段 - (instancetype)init { self = [super init]; if (self) { randomIDStr = @"itcast"; uploadID = @"uploadFile"; } return self; } #pragma mark - 私有方法 - (NSString *)topStringWithMimeType:(NSString *)mimeType uploadFile:(NSString *)uploadFile { NSMutableString *strM = [NSMutableString string]; [strM appendFormat:@"%@%@ ", boundaryStr, randomIDStr]; [strM appendFormat:@"Content-Disposition: form-data; name="%@"; filename="%@" ", uploadID, uploadFile]; [strM appendFormat:@"Content-Type: %@ ", mimeType]; NSLog(@"%@", strM); return [strM copy]; } - (NSString *)bottomString { NSMutableString *strM = [NSMutableString string]; [strM appendFormat:@"%@%@ ", boundaryStr, randomIDStr]; [strM appendString:@"Content-Disposition: form-data; name="submit" "]; [strM appendString:@"Submit "]; [strM appendFormat:@"%@%@-- ", boundaryStr, randomIDStr]; NSLog(@"%@", strM); return [strM copy]; } #pragma mark - 上传文件 - (void)uploadFileWithURL:(NSURL *)url data:(NSData *)data { // 1> 数据体 NSString *topStr = [self topStringWithMimeType:@"image/png" uploadFile:@"头像1.png"]; NSString *bottomStr = [self bottomString]; NSMutableData *dataM = [NSMutableData data]; [dataM appendData:[topStr dataUsingEncoding:NSUTF8StringEncoding]]; [dataM appendData:data]; [dataM appendData:[bottomStr dataUsingEncoding:NSUTF8StringEncoding]]; // 1. Request NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f]; // dataM出了作用域就会被释放,因此不用copy request.HTTPBody = dataM; // 2> 设置Request的头属性 request.HTTPMethod = @"POST"; // 3> 设置Content-Length NSString *strLength = [NSString stringWithFormat:@"%ld", (long)dataM.length]; [request setValue:strLength forHTTPHeaderField:@"Content-Length"]; // 4> 设置Content-Type NSString *strContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", randomIDStr]; [request setValue:strContentType forHTTPHeaderField:@"Content-Type"]; // 3> 连接服务器发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@", result); }]; } @end
// // MJViewController.m // 02.Post上传 #import "MJViewController.h" #import "UploadFile.h" @interface MJViewController () @end @implementation MJViewController - (void)viewDidLoad { [super viewDidLoad]; UploadFile *upload = [[UploadFile alloc] init]; NSString *urlString = @"http://localhost/upload.php"; NSString *path = [[NSBundle mainBundle] pathForResource:@"头像1.png" ofType:nil]; NSData *data = [NSData dataWithContentsOfFile:path]; [upload uploadFileWithURL:[NSURL URLWithString:urlString] data:data]; } @end