IOS: How to upload a file to specific Google drive

2019-02-07 03:17发布

I integrated Google drive sdk with my iOS app. But I do not know how to upload a file to Google drive specific folder.

Here the code am using to upload the file. But this one uploading the file to my google drive root folder. Any one share a code to upload a file to google drive specific folder?.

My Code:

-(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条回答
一纸荒年 Trace。
2楼-- · 2019-02-07 03:34

I don't know iOS, so may be off base, but does this code from https://developers.google.com/drive/v2/reference/files/insert help

+ (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];
查看更多
迷人小祖宗
3楼-- · 2019-02-07 03:43

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");

                                  }
                              }];
查看更多
迷人小祖宗
4楼-- · 2019-02-07 03:53

You need to set the parents property of your driveFile reference.

GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = folderIdentifier; // identifier property of the folder
driveFile.parents = @[ parentRef ];
查看更多
登录 后发表回答