NSURLSession文件上傳(前臺上傳)

NSURLSession提供的文件上傳接口,并不比NSURLConnection簡單,同樣需要在NSData中構(gòu)建HTTP固定的格式,下面介紹其用法。

配置session

  • 代碼
 NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];

 NSURLSession *downloadSession = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
  • 代碼說明
    將session配置為默認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è)置一個隨機的字符串.最后,設(shè)置了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]];
    
    // 文件內(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ù)不同的文件類型選擇不同的值

<空行>

<二進制數(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);
}

當數(shù)據(jù)傳送完成之后,系統(tǒng)回根據(jù)服務(wù)端返回的response進行處理

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    if ( error ) {
        NSLog(@"存在錯誤%@",error);
        return;
    }
    NSLog(@"文件上傳完成");
}

NSURLSession的任何task完成之后,都會執(zhí)行該回調(diào)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容