Your workflow is unique 👨‍💻 -  tell us how you use Dropbox here.

Forum Discussion

gowtham k.'s avatar
gowtham k.
New member | Level 1
11 years ago

how to download files from dropbox and show to user in iOS

Hello,
i need to show all files to user his dropbox account when user selects any file i need to download the file for that i have created a directory with name Dropbox as shown below.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
dataPath = [documentsDirectory stringByAppendingPathComponent:@"/Dropbox"];

NSError *error;

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
  [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil   error:&error];

self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
self.restClient.delegate = self;
[self.restClient loadMetadata:@"/"];

i was checking with metadata whether it is folder or file if folder i was loading file into my destination path as below.

- (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
    if (metadata.isDirectory) {
        NSLog(@"Folder '%@' contains:", metadata.path);
        for (DBMetadata *file in metadata.contents) {
            NSLog(@"file name%@", file.filename);
            if (file.isDirectory) {

                [self.restClient loadMetadata:[NSString stringWithFormat:@"/%@",file.filename]];

            }
           [self.restClient loadFile:file.path intoPath:dataPath];
        }
    }
}

when i tried to access the files in my directory there are no contents

NSString *extension = @"jpg";
NSFileManager *fileManager = [[NSFileManager alloc]init];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *Kdatatype = [documentsDirectory stringByAppendingPathComponent:@"/Dropbox"];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:Kdatatype error:nil];
NSMutableArray *jpegFiles = [NSMutableArray arrayWithCapacity: [contents count]];

NSString *filename;
for (filename in contents)        
{
    if ([[filename pathExtension] isEqualToString:extension])
    {
        [jpegFiles addObject:[UIImage imageNamed:[dataPath stringByAppendingPathComponent:filename]]];
    }
}

Query:
can any one help me out how to show user Dropbox files and download user selected file.

1 Reply

Replies have been turned off for this discussion
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    11 years ago

    So, if I understand correctly, the issue is that you're calling loadFile but you're not seeing anything downloaded to your local folder? Have you implemented the callbacks so you can see what's happening with those downloads?

    - (void)restClient:(DBRestClient*)client loadedFile:(NSString*)destPath
    - (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error;
    

    Also, you shouldn't call loadMetadata recursively as you have here, since that can cause a large number of calls unexpectedly. You should only call it when necessary to load the metadata for any particular desired level.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!