**************************关于文件上传的总结**************************
1.环境:Xcode7 iOS9.0
2.info.plist的配置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
文件上传步骤
// 1.创建url(服务器上传文件的url)
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/upload"];
// 2.获取文件名称
NSString *fileName = @"HTTP.txt";
// 3.获取文件类型
NSString *mimeType = [self mimeTypeWithUrl:fileUrl
// 4.获取文件数据
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
NSData *fileData = [NSData dataWithContentsOfURL:fileUrl];
];
// 5.设置其他参数
NSDictionary *parameters = @{
@"username" : @"iphone5solo",
@"age" : @10,
@"pwd" : @"1234352",
@"height" : @1.90
};
// 6.开启上传
[self uploadWithUrl:url fileData:fileData fileName:fileName mimeType:mimeType parameters:parameters];
/**
*获得MIMEType
*/
- (NSString *)mimeTypeWithUrl:(NSURL *)url
{
NSURLResponse *response = nil;
[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
response = response;
}];
return response.MIMEType;
}
/**
* url : 要上传的文件路径
* fileData : 文件的具体数据
* fileName : 上传文件的名称
* mimeType : 上传文件的类型
* parameters : 配置其他参数
*/
- (void)uploadWithUrl:(NSURL *)url fileData:(NSData *)fileData fileName:(NSString *)fileName mimeType:(NSString *)mimeType parameters:(NSDictionary *)parameters
{
// 1.创建POST请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 2.1设置请求方式
request.HTTPMethod = @"POST";
// 2.2设置请求体
NSMutableData *body = [NSMutableData data];
// 文件参数
[body appendData:STRING_TO_DATA(@"--")];
[body appendData:STRING_TO_DATA(MARK)];
[body appendData:CHANGE_LINE];
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"", fileName];
[body appendData:STRING_TO_DATA(disposition)];
[body appendData:CHANGE_LINE];
NSString *type = [NSString stringWithFormat:@"Content-Type: %@", mimeType];
[body appendData:STRING_TO_DATA(type)];
[body appendData:CHANGE_LINE];
[body appendData:CHANGE_LINE];
// 获取图片数据
[body appendData:fileData];
[body appendData:CHANGE_LINE];
[parameters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
// 用户名参数(非文件参数)
[body appendData:STRING_TO_DATA(@"--")];
[body appendData:STRING_TO_DATA(MARK)];
[body appendData:CHANGE_LINE];
NSString *keyStr = [NSString stringWithFormat:@" Content-Disposition: form-data; name=\"%@\"", key];
[body appendData:STRING_TO_DATA(keyStr)];
[body appendData:CHANGE_LINE];
[body appendData:CHANGE_LINE];
[body appendData:STRING_TO_DATA([obj description])];
[body appendData:CHANGE_LINE];
}];
// 结束标记
[body appendData:STRING_TO_DATA(@"--")];
[body appendData:STRING_TO_DATA(MARK)];
[body appendData:STRING_TO_DATA(@"--")];
[body appendData:CHANGE_LINE];
// 设置请求体
request.HTTPBody = body;
// 2.4设置请求头
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", MARK];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
// 3.创建NSURLSession 和 task
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
}];
// 4.开启任务
[task resume];
}
1.环境:Xcode7 iOS9.0
2.info.plist的配置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
文件上传步骤
// 1.创建url(服务器上传文件的url)
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/upload"];
// 2.获取文件名称
NSString *fileName = @"HTTP.txt";
// 3.获取文件类型
NSString *mimeType = [self mimeTypeWithUrl:fileUrl
// 4.获取文件数据
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
NSData *fileData = [NSData dataWithContentsOfURL:fileUrl];
];
// 5.设置其他参数
NSDictionary *parameters = @{
@"username" : @"iphone5solo",
@"age" : @10,
@"pwd" : @"1234352",
@"height" : @1.90
};
// 6.开启上传
[self uploadWithUrl:url fileData:fileData fileName:fileName mimeType:mimeType parameters:parameters];
/**
*获得MIMEType
*/
- (NSString *)mimeTypeWithUrl:(NSURL *)url
{
NSURLResponse *response = nil;
[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
response = response;
}];
return response.MIMEType;
}
/**
* url : 要上传的文件路径
* fileData : 文件的具体数据
* fileName : 上传文件的名称
* mimeType : 上传文件的类型
* parameters : 配置其他参数
*/
- (void)uploadWithUrl:(NSURL *)url fileData:(NSData *)fileData fileName:(NSString *)fileName mimeType:(NSString *)mimeType parameters:(NSDictionary *)parameters
{
// 1.创建POST请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 2.1设置请求方式
request.HTTPMethod = @"POST";
// 2.2设置请求体
NSMutableData *body = [NSMutableData data];
// 文件参数
[body appendData:STRING_TO_DATA(@"--")];
[body appendData:STRING_TO_DATA(MARK)];
[body appendData:CHANGE_LINE];
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"", fileName];
[body appendData:STRING_TO_DATA(disposition)];
[body appendData:CHANGE_LINE];
NSString *type = [NSString stringWithFormat:@"Content-Type: %@", mimeType];
[body appendData:STRING_TO_DATA(type)];
[body appendData:CHANGE_LINE];
[body appendData:CHANGE_LINE];
// 获取图片数据
[body appendData:fileData];
[body appendData:CHANGE_LINE];
[parameters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
// 用户名参数(非文件参数)
[body appendData:STRING_TO_DATA(@"--")];
[body appendData:STRING_TO_DATA(MARK)];
[body appendData:CHANGE_LINE];
NSString *keyStr = [NSString stringWithFormat:@" Content-Disposition: form-data; name=\"%@\"", key];
[body appendData:STRING_TO_DATA(keyStr)];
[body appendData:CHANGE_LINE];
[body appendData:CHANGE_LINE];
[body appendData:STRING_TO_DATA([obj description])];
[body appendData:CHANGE_LINE];
}];
// 结束标记
[body appendData:STRING_TO_DATA(@"--")];
[body appendData:STRING_TO_DATA(MARK)];
[body appendData:STRING_TO_DATA(@"--")];
[body appendData:CHANGE_LINE];
// 设置请求体
request.HTTPBody = body;
// 2.4设置请求头
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", MARK];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
// 3.创建NSURLSession 和 task
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
}];
// 4.开启任务
[task resume];
}