IOS:如何使用Google驱动器sdk库将文件上传到特定的Google云端硬盘文件夹

时间:2012-11-18 10:51:40

标签: ios google-drive-api

我将Google云端硬盘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");            
    }
}];
}

3 个答案:

答案 0 :(得分:10)

您需要设置parents引用的driveFile属性。

GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = folderIdentifier; // identifier property of the folder
driveFile.parents = @[ parentRef ];

答案 1 :(得分: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];

答案 2 :(得分: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");

                                  }
                              }];