IOS:如何使用 Google Drive sdk 库将文件上传到特定的 Google Drive 文件夹
Posted
技术标签:
【中文标题】IOS:如何使用 Google Drive sdk 库将文件上传到特定的 Google Drive 文件夹【英文标题】:IOS: How to upload a file to specific Google drive folder using Google drive sdk library 【发布时间】:2012-11-18 10:51:39 【问题描述】:我将 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");
];
【问题讨论】:
NSString *filePath = [LocalFilesDetails getUserDocumentFullPathForFileName:fileName isSignedDocument:YES];如何添加localfilesdetails @GamiNilesh 忘记了 LocalFilesDetails,只需在 filePath 变量中传递您的文件数据存储路径即可。没关系 【参考方案1】:您需要设置driveFile
引用的parents
属性。
GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = folderIdentifier; // identifier property of the folder
driveFile.parents = @[ parentRef ];
【讨论】:
我怎么知道文件夹标识符? @ValeriyVan 你从文件夹中得到它。大概您会在之前的查询中为您提供文件夹列表。 @rmaddy,我在哪里可以读到这个? Google 关于 Google Drive API 的文档并没有就“如果我知道文件夹名称如何将文件保存在特定文件夹中”这样简单的问题给出简单直接的答案。【参考方案2】:我不了解 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];
【讨论】:
我也查看了这段代码,但他们没有在该代码中使用“parentId”参数。还有其他想法吗? 我在哪里可以得到我需要的文件夹的parentId
?【参考方案3】:
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");
];
【讨论】:
你不觉得这个[NSString stringWithFormat:@"%@", title];
很有趣吗?为什么不只是title
?以上是关于IOS:如何使用 Google Drive sdk 库将文件上传到特定的 Google Drive 文件夹的主要内容,如果未能解决你的问题,请参考以下文章
google drive SDK for iOS 是不是支持 64 位架构?
iOS Google Drive SDK 获取子文件夹 ID