【问题标题】:Cannot upload .doc file with google drive sdk无法使用 google drive sdk 上传 .doc 文件
【发布时间】:2013-06-25 19:08:57
【问题描述】:

我可以从我的应用程序将 .docx 文件上传到谷歌驱动器,但是当我尝试上传 .doc 文件时,出现了这个错误:

发生错误:调用 POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&key=AIzaSyCm1WqPp05lBRjKSpxdtHjS8lz6WLeoWlU 时出错:(400) 提供的 mime 类型无效

我上传 .ppt .xls 文件时也出现了该错误。文档说我们可以在 Google Drive 中存储任何 MIME 类型。这里有什么问题?有人知道吗?

这是我的上传功能:

function insertFile($title, $description, $parentId, $mimeType, $filename) {

$file = new DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);

if ($parentId != null) {
  $parent = new ParentReference();
  $parent->setId($parentId);
  $file->setParents(array($parent));
}

try {
  $data = file_get_contents($filename);

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

  return $createdFile;
} catch (Exception $e) {
  print "An error occurred: " . $e->getMessage();
  }    
}

[更新] 我正在使用 CI,这是我在控制器中调用 insertFile 函数的函数:

function upload() {
$title = $this->input->post('title');
        $description = $this->input->post('description');
        $parentId = $this->input->post('parentId');

        if ($_FILES["file"]["error"] > 0) {
            echo "Error: " . $_FILES["file"]["error"] . "<br>";
        } else {
            $driveHandler = new DriveHandler($_SESSION['credentials']);
            $driveHandler->BuildService($_SESSION['credentials']);
            $ext = substr(strrchr($_FILES["file"]["name"],'.'),1);        
            if($title === ""){
                $fileTitle = $_FILES["file"]["name"];                
            } else {
                $fileTitle = "$title.$ext";
            }
            $driveHandler->insertFile($fileTitle, $description, $parentId, $_FILES["file"]["type"], $_FILES["file"]["tmp_name"]);              
}

【问题讨论】:

  • $mimeType 等于什么?
  • $mimeType = $_FILES["file"]["type"]。在我看来,我有这个:form_upload('file');
  • 否,失败时的实际值——错误是抱怨 MIME 类型。
  • Google Drive 应该接受任何 mime 类型,我的猜测是错误实际上在 $this-&gt;service-&gt;files-&gt;insert()
  • @doublesharp 原来错误在我的$mimeType 中。 .doc 文件的 $mimetype 中有一个额外的双引号。感谢您回答我的问题

标签: php google-drive-api


【解决方案1】:

当我尝试将var_dump($mimeType) 用于.docx 文件时,结果是:

string(71) "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

对于.doc 文件:

string(20) ""应用程序/msword""

根据结果,我发现了问题。 $mimeType 中有一个额外的双引号用于 .doc 文件。所以我尝试像这样更改我的代码:

  $data = file_get_contents($filename);
  $mime = str_replace('"', "", $mimeType);

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

现在可以了。

【讨论】:

    【解决方案2】:

    您可以在正则表达式中使用带有preg_replace() 的负字符类来替换任何非字母数字、/. 字符,而不仅仅是替换双引号。

    $mimeType = preg_replace("~[^a-zA-Z0-9/\.]*~", "", $mimeType);
    

    【讨论】:

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