NSURLConnection实现文件上传:
//文件上传步骤
/*
1.设置请求头
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryjv0UfA04ED44AhWx
2.按照固定的格式拼接请求体的数据
------WebKitFormBoundaryjv0UfA04ED44AhWx
Content-Disposition: form-data; name="file"; filename="Snip20160225_341.png"
Content-Type: image/png
------WebKitFormBoundaryjv0UfA04ED44AhWx
Content-Disposition: form-data; name="username"
123456
------WebKitFormBoundaryjv0UfA04ED44AhWx--
*/
//拼接请求体的数据格式
/*
拼接请求体
分隔符:----WebKitFormBoundaryjv0UfA04ED44AhWx
1)文件参数
--分隔符
Content-Disposition: form-data; name="file"; filename="Snip20160225_341.png"
Content-Type: image/png(MIMEType:大类型/小类型)
空行
文件参数
2)非文件参数
--分隔符
Content-Disposition: form-data; name="username"
空行
123456
3)结尾标识
--分隔符--
*/
#import"ViewController.h"
#define Kboundary @"----WebKitFormBoundaryjv0UfA04ED44AhWx"
#define KNewLine [@"\r\n"dataUsingEncoding:NSUTF8StringEncoding]
@interfaceViewController ()
@end
@implementationViewController
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[selfupload];
}
-(void)upload
{
//1.确定请求路径
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/upload"];
//2.创建可变的请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//3.设置请求方法
request.HTTPMethod =@"POST";
//4.设置请求头信息
//Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryjv0UfA04ED44AhWx
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",Kboundary] forHTTPHeaderField:@"Content-Type"];
//5.拼接请求体数据
NSMutableData *fileData = [NSMutableData data];
//5.1文件参数
/*
--分隔符
Content-Disposition: form-data; name="file"; filename="Snip20160225_341.png"
Content-Type: image/png(MIMEType:大类型/小类型)
空行
文件参数
*/
[fileData appendData:[[NSString stringWithFormat:@"--%@",Kboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[fileData appendData:KNewLine];
//name:file服务器规定的参数
//filename:Snip20160225_341.png文件保存到服务器上面的名称
//Content-Type:文件的类型
[fileData appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"Snip20160225_341.png\""dataUsingEncoding:NSUTF8StringEncoding]];
[fileData appendData:KNewLine];
[fileData appendData:[@"Content-Type: image/png"dataUsingEncoding:NSUTF8StringEncoding]];
[fileData appendData:KNewLine];
[fileData appendData:KNewLine];
UIImage *image = [UIImage imageNamed:@"Snip20160225_341"];
//UIImage --->NSData
NSData *imageData = UIImagePNGRepresentation(image);
[fileData appendData:imageData];
[fileData appendData:KNewLine];
//5.2非文件参数
/*
--分隔符
Content-Disposition: form-data; name="username"
空行
123456
*/
[fileData appendData:[[NSString stringWithFormat:@"--%@",Kboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[fileData appendData:KNewLine];
[fileData appendData:[@"Content-Disposition: form-data; name=\"username\""dataUsingEncoding:NSUTF8StringEncoding]];
[fileData appendData:KNewLine];
[fileData appendData:KNewLine];
[fileData appendData:[@"123456"dataUsingEncoding:NSUTF8StringEncoding]];
[fileData appendData:KNewLine];
//5.3结尾标识
/*
--分隔符--
*/
[fileData appendData:[[NSString stringWithFormat:@"--%@--",Kboundary] dataUsingEncoding:NSUTF8StringEncoding]];
//6.设置请求体
request.HTTPBody = fileData;
//7.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *_Nullableresponse, NSData *_Nullabledata, NSError *_NullableconnectionError) {
//8.解析数据
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}];
}
@end
获得文件的MIMEType:
//1.发送请求,可以响应头(内部有MIMEType)
//2.百度
//3.调用C语言的API
//4.application/octet-stream任意的二进制数据类型
-(void)getMimeType
{
//1.url
NSURL*url = [NSURLfileURLWithPath:@"/Users/apple/Desktop/123.h"];
//2.创建请求对象
NSURLRequest*request = [NSURLRequestrequestWithURL:url];
//3.发送异步请求
[NSURLConnectionsendAsynchronousRequest:requestqueue:[NSOperationQueuemainQueue]completionHandler:^(NSURLResponse*_Nullableresponse,NSData*_Nullabledata,NSError*_NullableconnectionError) {
//4.获得文件的类型
NSLog(@"%@",response.MIMEType);
}];
}
- (NSString*)mimeTypeForFileAtPath:(NSString*)path
{
if(![[[NSFileManageralloc]init]fileExistsAtPath:path]) {
returnnil;
}
CFStringRefUTI =UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridgeCFStringRef)[pathpathExtension],NULL);
CFStringRefMIMEType =UTTypeCopyPreferredTagWithClass(UTI,kUTTagClassMIMEType);
CFRelease(UTI);
if(!MIMEType) {
return@"application/octet-stream";
}
return(__bridgeNSString*)(MIMEType);
}
@end