如果文件不存在,如何将文件上传到谷歌驱动器?

时间:2014-09-23 15:38:12

标签: objective-c ios7 google-drive-api

我使用xcode开发iphone应用程序。

我使用此代码将数据库文件上传到我的Google云端硬盘。

- (void)uploadDB{
    GTLDriveFile *file = [GTLDriveFile object];
    file.title = [NSString  stringWithFormat:@"User:%@DB.sqlite",UserName];
    file.descriptionProperty = @"Uploaded from  iOS ";
    file.mimeType = @"application/x-sqlite3";


    // Attach a DB file to the email

    NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    //make a file name to write the data to using the documents directory:
    NSString *fileName = [NSString stringWithFormat:@"%@/%@",
                          documentsDirectory,DB_File];
    NSData   *data = [NSData dataWithContentsOfFile:fileName];


    GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithData:data MIMEType:file.mimeType];
    GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:file
                                                       uploadParameters:uploadParameters];

    UIAlertView *waitIndicator = [self showWaitIndicator:@"Uploading to Google Drive"];

    [self.driveService executeQuery:query
                  completionHandler:^(GTLServiceTicket *ticket,
                                      GTLDriveFile *insertedFile, NSError *error) {
                      [waitIndicator dismissWithClickedButtonIndex:0 animated:YES];
                      if (error == nil)
                      {
                          NSLog(@"File ID: %@", insertedFile.identifier);
                          [self showAlert:@"Google Drive" message:@"File saved!"];
                      }
                      else
                      {
                          NSLog(@"An error occurred: %@", error);
                          [self showAlert:@"Google Drive" message:@"Sorry, an error occurred!"];
                      }
                  }];

}

我的问题是它始终将其保存为新文件,我想知道它会检查驱动器上是否存在此文件名并替换它。

由于

1 个答案:

答案 0 :(得分:0)

云端硬盘中文件的标题不是唯一的。相反,您应该使用资源ID。第一次上传数据库文件时,请记住它在成功时返回给您的资源ID,然后从那时开始更新。