2

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

这里是代码。但是这段代码发生了错误。请帮我。-错误消息:Domain=com.google.GTLJSONRPCErrorDomain Code=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)
 {..}
4

2 回答 2

0

只是为了确定:您是否尝试过正确的 mime 类型?您的帖子中有一个错字:“application/vnd.google-apps.docuemnt”,这不是他们的 API 可接受的 mime 类型

于 2013-12-13T14:39:59.503 回答
0

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

于 2012-12-19T19:48:37.423 回答