美文网首页
AFNetworking post请求下载文件

AFNetworking post请求下载文件

作者: Yancy007 | 来源:发表于2019-01-30 18:18 被阅读0次
NSMutableDictionary *baseParameters = [self baseParametes];
        
        [baseParameters yxg_setObject:apikey forKey:@"apikey"];
        [baseParameters yxg_setObject:secret forKey:@"secret"];
        [baseParameters yxg_setObject:objectname forKey:@"objectname"];
        
        // 网络加载标志
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        NSString *downloadPath = [NSString stringWithFormat:@"%@%@",URL_OssFile,url];
        /* 创建网络下载对象 */
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        /* 下载地址 */
        NSURL *url = [NSURL URLWithString:downloadPath];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        request.HTTPMethod = @"POST";
        NSString *bodyStr = [NSString stringWithFormat:@"apikey=%@&secret=%@&objectname=%@",apikey,secret,objectname];
        request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
        /* 下载路径 */
        NSString *localfilePath = [[SISZIPManager defultDownloadZipPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",objectname]];
        /* 开始请求下载 */
        NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
            NSLog(@"下载进度:%.0f%", downloadProgress.fractionCompleted * 100);
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
            /* 设定下载到的位置 */
            return [NSURL fileURLWithPath:localfilePath];
        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            if (error) {
                failed(@"下载失败");
            } else {
                callback(localfilePath);
            }
        }];
        [downloadTask resume];

相关文章

网友评论

      本文标题:AFNetworking post请求下载文件

      本文链接:https://www.haomeiwen.com/subject/dficsqtx.html