【问题标题】:How to upload/convert *.docx to google documents? Google Drive Api v3如何将 *.docx 上传/转换为谷歌文档?谷歌云端硬盘 API v3
【发布时间】:2018-01-17 11:46:01
【问题描述】:

我在上传 microsoft/libre office 文档以通过 Google 应用程序进行编辑时遇到问题。对于电子表格,它可以正常工作,但对于文档则不行。

当我上传带有 *.odt、*.docx 等扩展名的文件时,我可以通过谷歌查看器在 Google Drive 上看到内容,但是当我点击使用文档顶部的按钮编辑时,Google Drive 会创建一个具有相同内容的新文件内容和名称,但我没有关于描述或任何能力的原始文件 ID 的信息。任何想法如何上传准备在线编辑的文档?

  private async Task<string> UploadFileToGoogleDrive(string filePath, GoogleDriveFile file)
    {
        var fileData = new Google.Apis.Drive.v3.Data.File()
        {
            Name = file.Name,
            MimeType = file.MimeType

        };
        if (!String.IsNullOrEmpty(file.ParentId))
            fileData.Parents = new List<string>() { file.ParentId };

        FilesResource.CreateMediaUpload request;

        using (var stream = new FileStream(filePath, FileMode.Open))
        {
             request = _driveService.Files.Create(fileData, stream, StringEnum.GetStringValue(file.FileContent));
             request.Fields = "id";

             var uploadProgress = await request.UploadAsync();
             if (uploadProgress.Exception != null)
                 throw uploadProgress.Exception; 
        }
        return request.ResponseBody.Id;
    }

文件 mime 类型 https://developers.google.com/drive/v3/web/manage-downloads

或使用“importFormats”参数。 https://developers.google.com/drive/v3/reference/about/get

【问题讨论】:

  • 请编辑您的问题并包含您用于上传文件的代码。理想情况下包括stackoverflow.com/help/mcve
  • 这是来自 Google Drive 文档的基本上传。
  • 尝试设置 MimeType = "application/vnd.google-apps.document"
  • 使用 MimeType = "application/vnd.google-apps.document" 我收到错误 400 错误请求。使用 MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" 有效,但我只能在线查看文件内容而不能编辑。
  • 在 V2 中有一个命令会告诉 Google Drive 在上传时将文件转换为 google drive 文件类型,然后您可以对其进行编辑。我一直在挖掘我似乎找不到的文档。搜索名为 convert 的东西。

标签: c# google-api google-drive-api google-api-dotnet-client


【解决方案1】:

我也受了很长时间。 为了让 Google 转换为我们需要的格式,我们在元数据中的 MimeType 中指定它。您还需要在内容对象的 MimeType 中指定当前文件格式。

只有 PHP 有一个例子

$fileMetadata = new \Google_Service_Drive_DriveFile([
        'name' => $this->_fileName,
        'parents' => array($directoryId),
        'mimeType' => 'application/vnd.google-apps.document'
    ]);

    return $this->_service()->files->create($fileMetadata, [
        'data' => $document,
        'mimeType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'uploadType' => 'multipart',
        'fields' => 'id',
    ]);

【讨论】:

    【解决方案2】:

    这是在 C# 中的(我从 https://developers.google.com/drive/api/v3/manage-uploads#import_to_google_docs_types_ 遵循了这个示例

    您应该能够将代码放在文件流的 using 语句中

        using File = Google.Apis.Drive.v3.Data.File; // This obviously goes at the top of the file, alternatively you can fully qualify the File constructor, although I had ambiguity issued due to also `using System.IO` and the compiler not knowing which `File` I meant specifically. 
    
        ...
    
        var fileMetadata = new File() // please note this is Google.Apis.Drive.v3.Data.File;
        {
            Name = "New File Name I Want"
            MimeType = "application/vnd.google-apps.document"
        };
    
        var request = service.Files.Create(
            fileMetadata, 
            stream, 
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document" // please note this is the mimeType of the source file not the tartget
        ); 
        request.Fields = "id";
        var result = await request.UploadAsync();
    

    根据文档,可以使用更新操作而不是上传来执行此操作,但这会转换原始文件并且原始文件会丢失。不幸的是,没有给出关于如何执行此操作的示例,并且 API 似乎对传入的内容非常挑剔。如果我弄明白了,将更新此答案以包括

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多