dongmeng2509 2015-03-23 14:43
浏览 68
已采纳

上传的文件内容缺少Google云端硬盘

Info:

I've made a short sample in which I upload a file to Google Drive using the credentials of the service account. Since the service account doesn't have a Drive UI (Google Drive API Upload returns File ID/Title but document does not exist in Drive), I've added permissions for my own account. This works fine. But when I open the file in Google Docs I don't see the content of the file I uploaded. Below is my code. In the file I want to upload the content is "Hello World!" but this is not present in the document on Google Drive. Any ideas on how to fix this?

        function insertFile($service, $title, $description, $parentId, $mimeType, $fileName) {
            print "<br/>Uploading file...<br/>";
            $file = new Google_Service_Drive_DriveFile();
            $file->setTitle($title);
            $file->setDescription($description);
            $file->setMimeType($mimeType);

            // Set the parent folder
            if ($parentId != null) {
                $parent = new Google_Service_Drive_ParentReference();
                $parent->setId($parentId);
                $file->setParents(array($parent));
            }

            try {
                $data = file_get_contents($fileName);

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

                $newFileId = $createdFile->getId();

                // Give permission to my email so I can see the file in my Drive UI
                $myEmail = "<mygmail>";
                $userType = "user";
                $userRole = "writer";
                insertPermission($service, $newFileId, $myEmail, $userType, $userRole);

                print "<br/>File uploaded successfully!";
                downloadFile($service, $newFileId);
            } catch (Exception $e) {
                print "<br/>An error occurred: " . $e->getMessage();
            }
        }

        function insertPermission($service, $fileId, $userToPermit, $typeOfUser, $role) {
            $newPermission = new Google_Service_Drive_Permission();
            $newPermission->setValue($userToPermit);
            $newPermission->setType($typeOfUser);
            $newPermission->setRole($role);
            try {
                return $service->permissions->insert($fileId, $newPermission);
            } catch (Exception $e) {
                print "<br/>Error occurred while attempting to permit " . $userToPermit . " to file.<br/>" . $e->getMessage();
            }
            return null;
        }

        $docTitle = "UploadTest4.txt";
        $docMimeType = "text/plain";
        $docToUpload = "document.txt";
        insertFile($service, $docTitle, null, null, $docMimeType, $docToUpload);
  • 写回答

1条回答 默认 最新

  • dq62957 2015-03-23 14:52
    关注

    Didn't find this post before, but I tried the solution provided and it works for me (not for the original poster strangely enough). I had to add an uploadType parameter in the insert request.

    uploadType => 'media'
    

    file uploaded with empty content using google drive php api

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看