【问题标题】:Google Drive SDK (PHP Client Library) - Upload file but unreadable on the Google Drive UIGoogle Drive SDK(PHP 客户端库)- 上传文件但在 Google Drive UI 上不可读
【发布时间】:2012-08-05 17:03:35
【问题描述】:

这是我上传文件的代码:

private function addFile(File $file, $folderProject, User $user) {

    $fileToUpload = new Google_DriveFile();
    $fileToUpload->setTitle($file->getName());

    //set mime type
    $fileToUpload->setMimeType($file->getMimeType());

    //build parent and set id
    $parent = new Google_ParentReference();
    $parent->setId($folderProject->getId());

    //set parent to file
    $fileToUpload->setParents(array($parent));

    $data = file_get_contents($file->getPath());

    $createdFile = $this->service->files->insert($fileToUpload, array(
            'data' => $data,
            'mimeType' => $file->getMimeType(),
    ));

}

当我使用此 PHP 代码上传例如 docx 时,该文件附加在 Google Drive 网站上不可读(它显示 docx 的 XML 结构),并且它在我的 Google Drive 文件夹中显示如下:

当我尝试直接通过 Google Drive UI 上传 docx 时,它会显示如下(我想要的):

我很确定问题出在 Google Drive UI 尝试以不同的方式打开文件,因为我上传了 它通过我的应用程序,但我不希望这样:)

(我对其他类型的文件也有同样的问题)


我尝试在数组中设置参数'convert' = true,但在上传过程中出现此错误:

..convert=true&uploadType=multipart&key=AI39si49a86oqv9fhb9yzXp7FCWL-cSqwFiClHZ‌​qS3Y3r9Hw8ujtSNlZEWZyFKBp_id7LUyFaZTJ97pMeb0XqHznycK7JJ9o_Q: (500) 内部错误

文档:https://developers.google.com/drive/v2/reference/files/insert

=> 但无论如何我不认为转换是正确的解决方案,我只想能够看到内容,就好像我通过 Google Drive UI 上传文件一样


如果我从我的 Google Drive Sync 文件夹中打开 docx 并编辑并保存它,我将能够通过 Google Drive UI 看到它(显示 W 标志)

【问题讨论】:

  • 通过 Google Drive UI 上传的文件是否已转换为 Google 文档?我问是因为我没有 .docx 文件来测试它,但如果是这种情况,您可以通过在插入请求中提供 convert=true 查询参数来重现此问题:将 'convert' => TRUE 添加到 @ 987654328@.
  • MH没有我得到这个错误,则:错误调用POST ...转换=真uploadType =多&键= AI39si49a86oqv9fhb9yzXp7FCWL-cSqwFiClHZqS3Y3r9Hw8ujtSNlZEWZyFKBp_id7LUyFaZTJ97pMeb0XqHznycK7JJ9o_Q:(500)内部错误,忘记给您回复是的,它是从谷歌转换Drive UI 或什至使用适用于 Windows 的 Google Drive App(同步文件夹),我的意思是它仍然是一个 .docx,但您可以通过 Google Drive UI 阅读它;)
  • @Alain 这可能是一个错误,不是吗?

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


【解决方案1】:

我也遇到了同样的问题。

但不同的是,我正在上传 pdf 并且能够在 google drive UI 中查看 pdf。

我需要在添加新的pdf后通过api代码自动将其转换为google doc格式。

如果我更改 $mimeType = 'application/vnd.google-apps.document';然后得到以下错误
https://www.googleapis.com/upload/drive/v2/files?convert=true&uploadType=multipart: (400) Bad Request

如果我添加可选参数

$createdFile = $service->files->insert($file, array(
  'data' => $data,
  'mimeType' => $mimeType,
'convert' => TRUE
));

然后没有变化,只有pdf上传没有转换发生。

您有什么建议吗,我需要在哪里准确添加转换参数?

【讨论】:

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