简体   繁体   中英

Download a file from BOX SDK iOS

I need to download an image from the BOX sdk into my app. I have this already working using the dropbox sdk - seems easier than Box sdk. Anyway - I have a delegate method returning the name of the file but how do I actually download the file?

    - (void)itemsViewController:(BOXItemsViewController *)itemsViewController didTapFile:(BOXFile *)file inItems:(NSArray *)items {

    NSLog(@"Did tap file: %@", file.name);

    BOXFileDownloadRequest *downloadRequest;
    BOXContentClient *contentClient;

    contentClient = [BOXContentClient defaultClient];
    NSOutputStream *outputStream = [[NSOutputStream alloc] initToMemory];
    downloadRequest = [_contentClient fileDownloadRequestWithID:file.name toOutputStream:outputStream];
    [_downloadRequest performRequestWithProgress:^(long long totalBytesTransferred, long long totalBytesExpectedToTransfer) {
    } completion:^(NSError *error) {
        if (error == nil) {
            NSData *data = [outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
            UIImage *img = [UIImage imageWithData:data];
            _uiiv_logo.image = img;
        }
        else{
        }
    }];

}

I ended up casting to BOXItem to get the 'id'.

- (void)itemsViewController:(BOXItemsViewController *)itemsViewController didTapFile:(BOXFile *)file inItems:(NSArray *)items
{
    BOXItem *item = file;
    BOXContentClient *contentClient = [BOXContentClient defaultClient];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSString *localFilePath = [documentsPath stringByAppendingPathComponent:@"NSURLSession.png"];

    BOXFileDownloadRequest *boxRequest = [contentClient fileDownloadRequestWithID:[item.JSONData valueForKey:@"id"] toLocalFilePath:localFilePath];
    [boxRequest performRequestWithProgress:^(long long totalBytesTransferred, long long totalBytesExpectedToTransfer) {
        // Update a progress bar, etc.
        NSLog(@"progress %lld",totalBytesTransferred);
    } completion:^(NSError *error) {
        // Download has completed. If it failed, error will contain reason (e.g. network connection)
        if (error) {
            NSLog(@"error %@",[error description]);
            [[NSNotificationCenter defaultCenter] postNotificationName:@"customUpdateBG" object:nil];
        }
    }];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM