7

我将 Google drive sdk 与我的 iOS 应用程序集成在一起。但我不知道如何将文件上传到 Google 驱动器特定文件夹。

这是用于上传文件的代码。但是这个将文件上传到我的谷歌驱动器根文件夹。任何人共享代码以将文件上传到谷歌驱动器特定文件夹?

我的代码:

-(void)uploadFileToGoogleDrive:(NSString*)fileName
{    
GTLDriveFile *driveFile = [[[GTLDriveFile alloc]init] autorelease];

driveFile.mimeType = @"application/pdf";
driveFile.originalFilename = @"test.doc";
driveFile.title = @"test.doc";

NSString *filePath = [LocalFilesDetails getUserDocumentFullPathForFileName:fileName isSignedDocument:YES];

GTLUploadParameters *uploadParameters = [GTLUploadParameters
                                         uploadParametersWithData:[NSData dataWithContentsOfFile:filePath]
                                         MIMEType:@"application/pdf"];    

GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:driveFile
                                    uploadParameters:uploadParameters];

[self.driveService executeQuery:query 
  completionHandler:^(GTLServiceTicket *ticket,
                       GTLDriveFile *updatedFile,
                       NSError *error) {
    if (error == nil) {
        NSLog(@"\n\nfile uploaded into google drive\\<my_folder> foler");
    } else {
        NSLog(@"\n\nfile uplod failed google drive\\<my_folder> foler");            
    }
}];
}
4

3 回答 3

10

您需要设置参考的parents属性driveFile

GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = folderIdentifier; // identifier property of the folder
driveFile.parents = @[ parentRef ];
于 2012-11-18T20:59:22.700 回答
3

我不知道 iOS,所以可能不符合标准,但是https://developers.google.com/drive/v2/reference/files/insert中的这段代码有帮助吗

+ (void)insertFileWithService:(GTLServiceDrive *)service
                        title:(NSString *)title
                  description:(NSString *)description
                     parentId:(NSString *)parentId
                     mimeType:(NSString *)mimeType
                         data:(NSData *)data
              completionBlock:(void (^)(GTLDriveFile *, NSError *))completionBlock {
  GTLDriveFile *file = [GTLDriveFile object];

  file.title = title;
  file.descriptionProperty = description;
  file.mimeType = mimeType;

  GTLUploadParameters *uploadParameters =
    [GTLUploadParameters uploadParametersWithData:data MIMEType:mimeType];
  GTLQueryDrive *query =
    [GTLQueryDrive queryForFilesInsertWithObject:file
                                uploadParameters:uploadParameters];
于 2012-11-18T12:00:06.697 回答
1

GTLServiceTicket *_editFileListTicket;

    GTLServiceDrive *service = self.driveService;

    GTLDriveFile *folderObj = [GTLDriveFile object];

    folderObj.title = [NSString stringWithFormat:@"%@", title];

    folderObj.mimeType = @"application/vnd.google-apps.folder";

    GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:folderObj
                                                       uploadParameters:nil];
    _editFileListTicket = [service executeQuery:query
                              completionHandler:^(GTLServiceTicket *ticket,
                                                  GTLDriveFile *folderItem,
                                                  NSError *error) {

                                 if (error == nil)
                                 {

                                     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"Created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                                     [alert show];


                                    }
                                 else
                                 {

                                     NSLog(@"error");

                                  }
                              }];
于 2013-01-03T15:47:32.090 回答