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];
}