如何删除以前的文件并在iOS中的Dropbox中添加新文件?

时间:2016-03-31 11:49:17

标签: ios dropbox dropbox-api delete-file

我正在保存我的保管箱中的example.txt文件。当我在该txt文件中添加一些文本时,我想从Dropbox中删除以前的文件并添加新文件作为相同的example.txt名称。但它没有发生。

这是上传文件代码:

NSString *text = @"Hello Word !!!!";
NSString *filename = @"working-draft.txt";
NSString *localDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *localPath = [localDir stringByAppendingPathComponent:filename];
[text writeToFile:localPath atomically:YES encoding:NSUTF8StringEncoding error:nil];


[self.restClient deletePath:@"/working-draft.txt"]; // Here I am deleting Previous txt file and adding new file

// Upload file to Dropbox
NSString *destDir = @"/";
[self.restClient uploadFile:filename toPath:destDir withParentRev:nil fromPath:localPath];

委托方法:

#pragma mark Delegate method for Uploading file in dropbox --------

- (void)restClient:(DBRestClient *)client uploadedFile:(NSString *)destPath
          from:(NSString *)srcPath metadata:(DBMetadata *)metadata {
NSLog(@"File uploaded successfully to path: %@", metadata.path);
self.activityIndicate.hidden = YES;

}
- (void)restClient:(DBRestClient *)client uploadFileFailedWithError:(NSError *)error {
NSLog(@"File upload failed with error: %@", error);
}

在上传期间,我删除了之前的txt文件并上传了新文件。但有时首先添加名为example(2).txt的第二个文件,并在添加之后删除之前的文件。

我该如何解决这个问题。请任何人都可以帮助我。 ?

2 个答案:

答案 0 :(得分:0)

两件事:

  1. 您实际上并没有在上传之前等待删除完成。在尝试再次添加文件之前,您应该等到调用deletedPath代理(表示删除已完成)。
  2. 如果您要做的只是替换文件,则无需先删除它。您只需使用overwrite参数。
  3. 支持overwrite在v1 iOS SDK中并不是很好,所以我相信您需要使用uploadFile的重载来获取params字典,如下所示:

    NSMutableDictionary *params =
        [NSMutableDictionary dictionaryWithObjects:@"true"
                                            forKey:@"overwrite"];
    [self.restClient uploadFile:filename
                         toPath:destDir
                       fromPath:localPath
                         params:params];
    

答案 1 :(得分:0)

对于Swift 3。

如果要覆盖该文件:

client.files.upload(path: filePath, mode: .overwrite, autorename: false, clientModified: nil, mute: true, input: data).response(completionHandler: { (response, error) in
    if let metadata = response {
      aPrint("Uploaded file name: \(metadata.name)"); aPrint("Uploaded file revision: \(metadata.rev)")
      completionHandler(metadata, error)

    } else {
      completionHandler(nil, error)
    }

  })

如果要删除文件:

if let client = DropboxClientsManager.authorizedClient {
  client.files.delete(path: filePath).response(completionHandler: { (response, error) in
    if let metadata = response {
      aPrint("Uploaded file name: \(metadata.name)")
      completionHandler(metadata, error)
    }  else {
      completionHandler(nil, error)
    }
  })
}