Google云端硬盘iOS:上传HTML文件以覆盖现有的文档文件

时间:2014-06-17 11:28:06

标签: ios google-drive-api google-docs google-docs-api

我正在尝试编写一个应用程序,将HTML Docs文件的内容作为HTML下载,以便我在应用程序内编辑它。编辑html文件后,我想将更改上传回Google云端硬盘并更新原始Google文档文件的内容。我已经能够下载Google Docs文件,但无法将我的更改上传回服务器。

你能帮忙理解为什么会发生这种错误吗?并可能建议我解决这个问题?

我收到以下NSError:

    Error Domain=com.google.GTLJSONRPCErrorDomain Code=500 "The operation couldn’t be completed. (Internal Error)" UserInfo=0x157a8610 {error=Internal Error, GTLStructuredError=GTLErrorObject 0x16846f60: {message:"Internal Error" code:500 data:[1]}, NSLocalizedFailureReason=(Internal Error)}
2014-06-17 12:11:35.188 DrEdit[548:60b] Error UserInfo: {
    GTLStructuredError = "GTLErrorObject 0x16846f60: {message:\"Internal Error\" code:500 data:[1]}";
    NSLocalizedFailureReason = "(Internal Error)";
    error = "Internal Error";
}

请在下方上传时执行代码:

- (void)saveFile {
    GTLUploadParameters *uploadParameters = nil;

    // Only update the file content if different.
    if (![self.originalContent isEqualToString:self.textView.text]) {
//        NSData *fileContent =
//        [self.textView.text dataUsingEncoding:NSUTF8StringEncoding];

        NSAttributedString *s = self.textView.attributedText;

        NSDictionary *documentAttributes = [NSDictionary dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute, nil];
        NSData *htmlData = [s dataFromRange:NSMakeRange(0, s.length) documentAttributes:documentAttributes error:NULL];
//        NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];

//        NSData *fileContent = [self.textView.attributedText convertToData];
        uploadParameters = [GTLUploadParameters uploadParametersWithData:htmlData MIMEType:@"text/html"];

//        [GTLUploadParameters uploadParametersWithData:fileContent MIMEType:@"text/plain"];
//        [GTLUploadParameters uploadParametersWithData:fileContent MIMEType:@"application/vnd.google-apps.document"];
    }

    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);
            NSLog(@"Error UserInfo: %@", error.userInfo);
            [DrEditUtilities showErrorMessageWithTitle:@"Unable to save file"
                                               message:[error description]
                                              delegate:self];
        }
    }];
}

谢谢, 迈克尔

2 个答案:

答案 0 :(得分:1)

无法以编程方式将html写入gdoc。 目前它唯一可以手动粘贴html而不是api,不幸的是(奇怪的是)

答案 1 :(得分:1)

我能够通过在GTLQueryDrive类上将属性 convert 更改为YES来解决此问题。该文档声明它将尝试将正在上传的文件转换为原生Google Docs格式。

希望这会有所帮助。请参阅下面的SDK中描述的方法:

// Method: drive.files.update
// Updates file metadata and/or content
//  Required:
//   fileId: The ID of the file to update.
//  Optional:
**//   convert: Whether to convert this file to the corresponding Google Docs
//     format. (Default false)**
//   newRevision: Whether a blob upload should create a new revision. If false,
//     the blob data in the current head revision will be replaced. (Default
//     true)
//   ocr: Whether to attempt OCR on .jpg, .png, or .gif uploads. (Default false)
//   ocrLanguage: If ocr is true, hints at the language to use. Valid values are
//     ISO 639-1 codes.
//   pinned: Whether to pin the new revision. (Default false)
//   setModifiedDate: Whether to set the modified date with the supplied
//     modified date. (Default false)
//   sourceLanguage: The language of the original file to be translated.
//   targetLanguage: Target language to translate the file to. If no
//     sourceLanguage is provided, the API will attempt to detect the language.
//   timedTextLanguage: The language of the timed text.
//   timedTextTrackName: The timed text track name.
//   updateViewedDate: Whether to update the view date after successfully
//     updating the file. (Default true)
//  Upload Parameters:
//   Maximum size: 10GB
//   Accepted MIME type(s): */*
//  Authorization scope(s):
//   kGTLAuthScopeDrive
//   kGTLAuthScopeDriveFile
// Fetches a GTLDriveFile.
+ (id)queryForFilesUpdateWithObject:(GTLDriveFile *)object
                             fileId:(NSString *)fileId
                   uploadParameters:(GTLUploadParameters *)uploadParametersOrNil;

谢谢, 迈克尔