溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

AFURLSessionManager如何實(shí)現(xiàn)上傳下載功能

發(fā)布時(shí)間:2021-07-24 10:58:27 來(lái)源:億速云 閱讀:181 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

這篇文章主要介紹了AFURLSessionManager如何實(shí)現(xiàn)上傳下載功能,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

1、下載 Creating a Download Task

  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
  AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 
  NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"]; 
  NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 
  NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; 
    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; 
  } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { 
    NSLog(@"File downloaded to: %@", filePath); 
  }]; 
  [downloadTask resume];

2、上傳 Creating an Upload Task

 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
  AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 
  NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"]; 
  NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 
  NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"]; 
  NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { 
    if (error) { 
      NSLog(@"Error: %@", error); 
    } else { 
      NSLog(@"Success: %@ %@", response, responseObject); 
    } 
  }]; 
  [uploadTask resume];

3、批量上傳 Creating an Upload Task for a Multi-Part Request, with Progress

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
      [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil]; 
    } error:nil]; 
  AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
  NSURLSessionUploadTask *uploadTask; 
  uploadTask = [manager 
         uploadTaskWithStreamedRequest:request 
         progress:^(NSProgress * _Nonnull uploadProgress) { 
           // This is not called back on the main queue. 
           // You are responsible for dispatching to the main queue for UI updates 
           dispatch_async(dispatch_get_main_queue(), ^{ 
             //Update the progress view 
             [progressView setProgress:uploadProgress.fractionCompleted]; 
           }); 
         } 
         completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { 
           if (error) { 
             NSLog(@"Error: %@", error); 
           } else { 
             NSLog(@"%@ %@", response, responseObject); 
           } 
         }]; 
  [uploadTask resume];

4、數(shù)據(jù)任務(wù) Creating a Data Task

  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
  AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 
  NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/get"]; 
  NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 
  NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { 
    if (error) { 
      NSLog(@"Error: %@", error); 
    } else { 
      NSLog(@"%@ %@", response, responseObject); 
    } 
  }]; 
  [dataTask resume];

5、請(qǐng)求參數(shù)設(shè)置 Request Serialization

Request serializers create requests from URL strings, encoding parameters as either a query string or HTTP body.
  NSString *URLString = @"http://example.com"; 
  NSDictionary *parameters = @{@"foo": @"bar", @"baz": @[@1, @2, @3]};

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“AFURLSessionManager如何實(shí)現(xiàn)上傳下載功能”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI