NSURLSession提供的文件上傳接口,并不比NSURLConnection簡單,同樣需要在NSData中構建HTTP固定的格式,下面介紹其用法。
配置session
- 代碼
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *downloadSession = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
- 代碼說明
將session配置為默認session,并設置session的回調在主線程,不多說。
配置request
- 代碼
NSURL *url = [NSURL URLWithString:@"http://localhost/upload.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
request.HTTPMethod = @"post";
- 代碼說明
首先通過URL生成一個request,如果是上傳文件,header中的Content-Type的值必須設置為:multipart/form-data; boundary=xxxxxx,xxxxxx用于判斷文件的邊界,可以是任意值,一般設置一個隨機的字符串.最后,設置了httpMethod為post,文件上傳的默認方法為post,也可以是put,但不常用
配置上傳的文件
- 代碼
- (NSData *)getSendDataWithFilePath:(NSString *)path
{
NSMutableData *data = [NSMutableData data];
// 表單拼接
NSMutableString *headerStrM =[NSMutableString string];
[headerStrM appendFormat:@"--%@\r\n",boundary];
// name:表單控件名稱 filename:上傳文件名
[headerStrM appendFormat:@"Content-Disposition: form-data; name=%@; filename=%@\r\n",@"userfile",@"test.txt"];
[headerStrM appendFormat:@"Content-Type: %@\r\n\r\n",@"text/plain"];
[data appendData:[headerStrM dataUsingEncoding:NSUTF8StringEncoding]];
// 文件內容
NSData *fileData = [NSData dataWithContentsOfFile:path];
[data appendData:fileData];
NSMutableString *footerStrM = [NSMutableString stringWithFormat:@"\r\n--%@--\r\n",boundary];
[data appendData:[footerStrM dataUsingEncoding:NSUTF8StringEncoding]];
return data;
}
- 代碼說明
http協議上傳文件有固定的格式:
--boundary\r\n
Content-Disposition: form-data; name="<服務器端需要知道的名字>"; filename="<服務器端這個傳上來的文件名>"
Content-Type: application/zip --根據不同的文件類型選擇不同的值
<空行>
<二進制數據>
--boundary--\r\n
該格式是固定的,上述代碼也只是按照上述格式,封裝了一個NSData.需要注意的是Content-Disposition: form-data; name="<服務器端需要知道的名字>"; 這個需要服務端告訴name的值.設置好了這些遍可以上傳數據了.這個格式看似復雜,其實是固定的,設置正確就行.
開始上傳
NSURLSessionUploadTask *uploadTask = [uploadSession uploadTaskWithRequest:request fromData:[self getSendDataWithFilePath:filePath]];
[uploadTask resume];
生成一個上傳任務,并開始上傳,這沒什么可說的
處理回調
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{
NSLog(@"bytesSent=%@",@(bytesSent));
NSLog(@"totalBytesSent=%@",@(totalBytesSent));
NSLog(@"totalBytesExpectedToSend=%@",@(totalBytesExpectedToSend));
}
系統并沒有專門為上傳提供回調方法,但是我們能利用NSURLSessionTask的回調方法,上述方法是在數據傳輸過程中調用的方法.
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
NSLog(@"response=%@",response);
completionHandler(NSURLSessionResponseAllow);
}
當數據傳送完成之后,系統回根據服務端返回的response進行處理
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if ( error ) {
NSLog(@"存在錯誤%@",error);
return;
}
NSLog(@"文件上傳完成");
}
NSURLSession的任何task完成之后,都會執行該回調