如何使用iPhone版Google云端硬盘SDK在Google云端硬盘上创建文件夹? [英] how to create folder on Google Drive using Google Drive SDK for iPhone?

查看:163
本文介绍了如何使用iPhone版Google云端硬盘SDK在Google云端硬盘上创建文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iPhone的Google Drive SDK并尝试将音频文件上传到"TestAudio"文件夹中.如果未在Google驱动器上创建"TestAudio"文件夹,请先创建该文件夹,然后我的音频应仅存储到该文件夹​​中.除文件夹创建外,所有东西都可以正常工作.好友可以帮忙吗?

I am using Google Drive SDK for iPhone and trying to upload Audio file in "TestAudio" folder.If "TestAudio" folder is not created at google drive then first create that folder and after that my audio should store to that folder only. Every thing is working gr8 except folder creation. can any buddy please help?

我正在使用以下代码上传音频文件.

I am using below code for upload audio file.

GTLUploadParameters *uploadParameters = nil;

NSString *soundFilePath = [[NSBundle mainBundle]
                           pathForResource:@"honey_bunny_new"
                           ofType:@"mp3"];

if (soundFilePath) {
    NSData *fileContent = [[NSData alloc] initWithContentsOfFile:soundFilePath];
    uploadParameters = [GTLUploadParameters uploadParametersWithData:fileContent MIMEType:@"audio/mpeg"];
}

self.driveFile.title = self.updatedTitle;
GTLQueryDrive *query = nil;
if (self.driveFile.identifier == nil || self.driveFile.identifier.length == 0) {
    // This is a new file, instantiate an insert query.
    query = [GTLQueryDrive queryForFilesInsertWithObject:self.driveFile
                                        uploadParameters:uploadParameters];
} else {
    // This file already exists, instantiate an update query.
    query = [GTLQueryDrive queryForFilesUpdateWithObject:self.driveFile
                                                  fileId:self.driveFile.identifier
                                        uploadParameters:uploadParameters];
}
UIAlertView *alert = [DrEditUtilities showLoadingMessageWithTitle:@"Saving file"
                                                         delegate:self];

[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                          GTLDriveFile *updatedFile,
                                                          NSError *error) {
[alert dismissWithClickedButtonIndex:0 animated:YES];
if (error == nil) {
    self.driveFile = updatedFile;
    self.originalContent = [self.textView.text copy];
    self.updatedTitle = [updatedFile.title copy];
    [self toggleSaveButton];
    [self.delegate didUpdateFileWithIndex:self.fileIndex
                                    driveFile:self.driveFile];
    [self doneEditing:nil];
} else {
    NSLog(@"An error occurred: %@", error);
    [DrEditUtilities showErrorMessageWithTitle:@"Unable to save file"
                                           message:error.description
                                          delegate:self];
}
}];

推荐答案

我看不到您创建文件夹的代码,但是我自己在创建文件夹时遇到了同样的问题.如您所知,mimeType必须为"application/vnd.google-apps.folder".如果uploadDatasWithData的NSData参数为nil,我会遇到断言失败.所以我尝试了一个零长度的NSData对象,但是失败了.使用1字节的NSData对象也失败.诀窍是使用uploadParameters:nil调用queryForFilesUpdateWithObject.然后,文件夹创建工作正常.我还发现在末尾显示了Objective-C代码:

I don't see your code to create a folder, but I was having the same problem with folder creation myself. As you know, the mimeType must be "application/vnd.google-apps.folder". I ran into assert failures if the NSData parameter to uploadParametersWithData was nil. So I tried a zero length NSData object and that failed. Using a 1 byte NSData object also failed. The trick is to call queryForFilesUpdateWithObject with uploadParameters:nil. Then the folder creation works fine. I also discovered that the Objective-C code shown at the end of:

https://developers.google.com/drive/v2/reference/文件/插入

不正确. file.parents应该如下:

is incorrect. The file.parents should be as follows:

GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = parentID;
if (parentID.length>0) file.parents = [NSArray arrayWithObjects:parentRef,nil];

这篇关于如何使用iPhone版Google云端硬盘SDK在Google云端硬盘上创建文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆