Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS: How to upload a file to specific Google drive folder using Google drive sdk library

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");            
    }
}];
}
like image 808
loganathan Avatar asked Nov 18 '12 10:11

loganathan


People also ask

How do I upload a file to Google Drive?

When you upload a file to Google Drive, it will take up space in your Drive, even if you upload to a folder owned by someone else. Tap Add . Tap Upload. Find and tap the files you want to upload. If you want to upload files like Microsoft Word documents, you can change a setting to convert files.

How to upload to Google Drive using mobile data on iPhone?

Open the Settings app on your iPhone and select the Mobile Data menu. 2. There tap on Mobile Data Options. 3. On the next page, disable the toggle next to Low Data Mode. Now, you can upload to Google Drive using mobile data. On iPhone, when 20% battery is left, the phone asks you to enable the Low Power mode.

How do I upload files to Google workspace?

Sign up for a Google Workspace trial at no charge. You can upload, view, share, and edit files with Google Drive. When you upload a file to Google Drive, it will take up space in your Drive, even if you upload to a folder owned by someone else. Tap Add . Tap Upload. Find and tap the files you want to upload.

How to add files to Google Drive shared folders?

The ability to add files to Google Drive shared folders depends on your permission level. As an Owner, you can do anything in that folder. If someone else has shared a folder with you, you will either have View, Comment, or Edit access. In such cases, you can only upload files to that folder if you have Edit privileges.


2 Answers

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 ];
like image 79
rmaddy Avatar answered Sep 28 '22 01:09

rmaddy


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];
like image 37
pinoyyid Avatar answered Sep 28 '22 01:09

pinoyyid