AFNetworking - Using AFHTTPRequestOperation to download file?

后端 未结 1 1655
误落风尘
误落风尘 2021-02-01 22:50

Man, stumped on this one. Trying to download a file with AFNetworking and the best I can do is capture the progress bytes in the downloadProgressBlock. I\'ve tried many differ

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 23:15

    You can simply use this feature that is included on AFNetworking and even save it to disk - Notice the operation.outputStream:

    NSMutableURLRequest* rq = [api requestWithMethod:@"GET" path:@"YOUR/URL/TO/FILE" parameters:nil];
    AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:rq] autorelease];
    
    NSString* path=[@"/PATH/TO/APP" stringByAppendingPathComponent: imageNameToDisk];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
    
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    
        NSLog(@"SUCCCESSFULL IMG RETRIEVE to %@!",path)
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
        // Deal with failure
    }];
    

    EDIT : missed

    [operation start]; 
    

    EDIT : ...or if you use an AFHTTPClient * apiClient :

    // [apiClient enqueueHTTPRequestOperation:operation];
    

    0 讨论(0)
提交回复
热议问题