IOS:如何使用Google驱动器sdk库将数据(NSData)上传到特定的Google驱动器文档(application / vnd.google-apps.document)

时间:2012-12-19 07:42:29

标签: ios google-drive-api

我将Google云端硬盘sdk与我的iOS应用集成在一起。但我不知道如何将数据(NSData类型)上传到Google驱动器特定文档(文档的mimeType是'application / vnd.google-apps.document,我想要上传混合数据(图像+文本)。)

这里的代码。但是这段代码发生了错误。请帮我。 -error消息:Domain = com.google.GTLJSONRPCErrorDomain代码= 400(错误请求)

NSMutableData *body = [NSMutableData data];
NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];

GTLDriveFile *newFile = [GTLDriveFile object];
newFile.mimeType = @"application/vnd.google-apps.docuemnt";

// another text parameter
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter2\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"body text2" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

// image file
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"imagename\"; filename=\"image.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:UIImageJPEGRepresentation([UIImage imageNamed:@"folder1"], 1.0)]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

// text parameter
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter1\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"body text" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];[body appendData:@""]; GTLUploadParameters *uploadParameters = nil;


uploadParameters = [GTLUploadParameters uploadParametersWithData:body MIMEType:contentType];
GTLQueryDrive *query = nil;
    // This is a new file, instantiate an insert query.
    query = [GTLQueryDrive queryForFilesInsertWithObject:newFile uploadParameters:uploadParameters];

query.convert = YES;

[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                          GTLDriveFile *updatedFile,
                                                          NSError *error)
 {..}

2 个答案:

答案 0 :(得分:0)

您指定的mime类型:application / vnd.google-apps.document适用于Google文档文档。你不应该接触那些。您想要创建自己的文档。

答案 1 :(得分:0)

只是为了确定:你尝试过正确的mime类型吗?您的帖子中有一个拼写错误: “application / vnd.google-apps.docuemnt”,这是其API无法接受的mime类型