NSURLSession提供的文件上傳接口绸硕,并不比NSURLConnection簡單,同樣需要在NSData中構(gòu)建HTTP固定的格式,下面介紹其用法玻佩。
配置session
- 代碼
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *downloadSession = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
- 代碼說明
將session配置為默認(rèn)session,并設(shè)置session的回調(diào)在主線程,不多說。
配置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的值必須設(shè)置為:multipart/form-data; boundary=xxxxxx,xxxxxx用于判斷文件的邊界,可以是任意值税稼,一般設(shè)置一個隨機(jī)的字符串.最后,設(shè)置了httpMethod為post,文件上傳的默認(rèn)方法為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]];
// 文件內(nèi)容
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協(xié)議上傳文件有固定的格式:
--boundary\r\n
Content-Disposition: form-data; name="<服務(wù)器端需要知道的名字>"; filename="<服務(wù)器端這個傳上來的文件名>"
Content-Type: application/zip --根據(jù)不同的文件類型選擇不同的值
<空行>
<二進(jìn)制數(shù)據(jù)>
--boundary--\r\n
該格式是固定的,上述代碼也只是按照上述格式扰肌,封裝了一個NSData.需要注意的是Content-Disposition: form-data; name="<服務(wù)器端需要知道的名字>"; 這個需要服務(wù)端告訴name的值.設(shè)置好了這些遍可以上傳數(shù)據(jù)了.這個格式看似復(fù)雜,其實是固定的盗舰,設(shè)置正確就行.
開始上傳
NSURLSessionUploadTask *uploadTask = [uploadSession uploadTaskWithRequest:request fromData:[self getSendDataWithFilePath:filePath]];
[uploadTask resume];
生成一個上傳任務(wù)夷狰,并開始上傳,這沒什么可說的
處理回調(diào)
- (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));
}
系統(tǒng)并沒有專門為上傳提供回調(diào)方法,但是我們能利用NSURLSessionTask的回調(diào)方法,上述方法是在數(shù)據(jù)傳輸過程中調(diào)用的方法.
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
NSLog(@"response=%@",response);
completionHandler(NSURLSessionResponseAllow);
}
當(dāng)數(shù)據(jù)傳送完成之后爷绘,系統(tǒng)回根據(jù)服務(wù)端返回的response進(jìn)行處理
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if ( error ) {
NSLog(@"存在錯誤%@",error);
return;
}
NSLog(@"文件上傳完成");
}
NSURLSession的任何task完成之后进倍,都會執(zhí)行該回調(diào)