国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

AFURLSessionManager 上傳下載使用代碼說明

2019-10-22 18:28:40
字體:
來源:轉載
供稿:網友

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、數據任務 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、請求參數設置 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]}; 

總結

以上所述是小編給大家介紹的AFURLSessionManager 上傳下載使用代碼說明,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

 

注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 深圳市| 古浪县| 泊头市| 通榆县| 江源县| 民和| 滨州市| 隆化县| 建昌县| 贵溪市| 辽源市| 古田县| 陈巴尔虎旗| 五寨县| 和硕县| 松滋市| 射阳县| 通许县| 环江| 临武县| 江陵县| 长寿区| 平山县| 华池县| 鄂托克旗| 中方县| 潍坊市| 缙云县| 桐城市| 漠河县| 三河市| 临潭县| 云林县| 道孚县| 封丘县| 和硕县| 武清区| 伊金霍洛旗| 肥西县| 辉南县| 昭苏县|