IPhone DropBox API: how to upload a file?

The easiest question regarding dropbox integration in iPhone.

I followed DropBoxSDK setup and everything works fine. I can log in to my account and link it. Therefore, I set everything up correctly.

Now, I would like to use it to just download the file from dropBox and save it again. Think that you only want to sync ONE FILE (for simplicity) called "example.txt" which is located in the "Example" folder in my DropBox. The same โ€œexample.txtโ€ is saved locally on the iPhone in the Documents folder of my application.

The readme file dropBox offers vaguely the following code, which I find very cryptic and cannot see how to load or save the file:

2. Make an request on the rest client: [[self restClient] loadMetadata:@"/"]; 3. Implement the DBRestClientDelegate methods needed to get the results of the particular call you made: - (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata { NSLog(@"Loaded metadata!"); } - (void)restClient:(DBRestClient*)client metadataUnchangedAtPath:(NSString*)path { NSLog(@"Metadata unchanged!"); } - (void)restClient:(DBRestClient*)client loadMetadataFailedWithError:(NSError*)error { NSLog(@"Error loading metadata: %@", error); } 

So my (hopefully) simple question is: how can I:

  • check if there is an example folder in my Dropbox
  • if not, create it and save the example.txt file from the application documents to this examples folder
  • load example.txt
  • after program termination: save example.txt in DropBox

I cannot find the answer to these rather simple steps in the Dropbox documents on the website. The example they provided, I find it too confusing ... especially because it concerns downloading files and not saving them, as far as I can see.

I would be grateful for any help or suggestions on how to do this.

+7
source share
2 answers

Ok, I found this method to save my example.txt file:

 -(void) DBupload:(id)sender { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Example.txt"]; [self.restClient uploadFile:@"NoteBook.txt" toPath:@"/example" fromPath:filePath]; } 

It turns out that there is no need to create a folder, Dropbox will do it automatically for you if it does not exist.

This is for loading the same form file:

 -(void) DBdownload:(id)sender { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Example.txt"]; NSError *error; [self.restClient loadFile:@"/example/Example.txt" intoPath:filePath]; if (filePath) { // check if file exists - if so load it: NSString *tempTextOut = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; } } 

Hope this helps if you are struggling with a similar question.

+15
source

In the DBdownload function, you can skip checking by implementing the DBRestClientDelegate loadedFile and loadFileFailedWithError methods

+2
source

All Articles