【发布时间】: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->service->files->insert() -
@doublesharp 原来错误在我的$mimeType 中。 .doc 文件的 $mimetype 中有一个额外的双引号。感谢您回答我的问题
标签: php google-drive-api