我将 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)
{..}