【问题标题】:Google Drive API httpRequest download file dialog boxGoogle Drive API httpRequest 下载文件对话框
【发布时间】:2015-01-18 20:34:17
【问题描述】:

我是 PHP 新手,我一直在使用 Google Drive PHP API。

这是我的代码:

function downloadFile1($service,$downloadUrl,$client) {

    if ($downloadUrl) {
      $request = new Google_Http_Request($downloadUrl, 'GET', null, null);

      $SignhttpRequest = $client->getAuth()->sign($request);
      $httpRequest = $client->getIo()->makeRequest($SignhttpRequest);

  if ($httpRequest->getResponseHttpCode() == 200) {

    return $httpRequest->getResponseBody();

  } else {
  // An error occurred.
    return null;
  }
 } else {
  // The file doesn't have any content stored on Drive.
   return null;
 }
}

到目前为止一切顺利。但我想得到一个对话框来将文件保存到我的电脑(当我得到返回结果时)。我怎么能得到它?

[如果我执行一个

$content = $httpRequest->getResponseBody();
print_r($content);]

我可以在网页中看到文件的内容,但我想下载它!]

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    不要使用 PHP 将文件下载到服务器,而是在页面上向用户显示指向文件webContentLink 的锚点。单击该链接会将文件下载到用户的计算机。

    【讨论】:

    • 但如果我使用 webContentLink,我会收到错误:403 很抱歉,您无权访问此页面。我希望我的应用能够在不登录的情况下将证明文件下载给某人。
    • 在这种情况下,做您已经在做的事情,但设置 Content-Disposition 标头,以便您的浏览器显示“另存为”对话框:php.net/manual/en/function.http-send-content-disposition.php
    • 感谢您的帮助,但我知道如果用户拥有链接,我可以将 Google Drive 文件权限设置为公开。因此,在这种情况下,webContentLnk 可以工作,问题就解决了。再次感谢并抱歉打扰您;-)
    【解决方案2】:

    大家好,也许这段代码可以帮助人们使用谷歌驱动器作为一项服务,可以与 cronjob/cli 一起使用来上传、删除和下载无限文件大小的文件。

    它还没有完成,但它是 google apiclient 的包装器。 并可用于上传、下载和删除文件。

    <?php
    
    namespace App\Model\Google;
    
    class Drive
    {
    public $pixie;
    
    protected $_config;
    protected $_client;
    protected $_service;
    
    public function __construct(\App\Pixie $pixie)
    {
        $this->pixie = $pixie;
        $this->_config = $this->pixie->config->get('googledrive');
    
        $this->_client = new \Google_Client();
        $this->_client->setClientId($this->_config['clientId']);
        $this->_client->setClientSecret($this->_config['clientSecret']);
        $this->_client->setRedirectUri($this->_config['redirectUri']); //
        $this->_client->addScope($this->_config['scope']); // scope: https://www.googleapis.com/auth/drive
        $this->_client->setAccessType($this->_config['accessType']); // offline
        $token = $this->_client->refreshToken($this->_config['refreshToken']);
    
        $this->_service = new \Google_Service_Drive($this->_client);
    }
    
    /**
     * UPLOAD FILE
     */
    public function uploadFile($filename, $file)
    {
        $gdriveFile = new \Google_Service_Drive_DriveFile();
        $gdriveFile->title = $filename;
    
        $chunkSizeBytes = 1 * 1024 * 1024;
    
        // Call the API with the media upload, defer so it doesn't immediately return.
        $this->_client->setDefer(true);
        $request = $this->_service->files->insert($gdriveFile);
        $mimeType = 'text/plain';
        // Create a media file upload to represent our upload process.
        $media = new \Google_Http_MediaFileUpload(
          $this->_client,
          $request,
          $mimeType,
          null,
          true,
          $chunkSizeBytes
        );
    
        $media->setFileSize(filesize($file));
    
        // Upload the various chunks. $status will be false until the process is
        // complete.
        $status = false;
        $handle = fopen($file, "rb");
        while (!$status && !feof($handle)) {
            $chunk = fread($handle, $chunkSizeBytes);
            $status = $media->nextChunk($chunk);
        }
    
        // The final value of $status will be the data from the API for the object
        // that has been uploaded.
        $result = false;
        if ($status !== false) {
            $result = $status;
        }
    
        return $result;
    }
    
    /**
     * DOWNLOAD FILE
     */
    public function downloadUrl($fileId)
    {
        $file = $this->getFile($fileId);
    
        if ($url = $file->getWebContentLink()) {
            return $url;
        }
    
        return false;
    }
    
    /**
     * GET FILE
     *   $file = $service->files->get($fileId);
     */
    public function getFile($fileId)
    {
        try {
            return $this->_service->files->get($fileId);
        } catch(\Exception $e) {
            throw new \Exception('Google Drive File Does Not Exist');
        }
    }
    
    /**
     * DELETE FILE
     *   $service->files->delete($fileId);
     */
    public function deleteFile($fileId)
    {
        new \Debug();
    }
    
    /**
     * FILE TO TRASH
     *   $service->files->trash($fileId);
     */
    public function trashFile($fileId)
    {
        new \Debug();
    }
    
    /**
     * EMPTY TRASH
     *   $service->files->emptyTrash();
     */
    public function emptyTrash()
    {
        new \Debug();
    }
    
    /**
     * FILE UNTRASH
     *   $service->files->untrash($fileId);
     */
    public function untrashFile($fileId)
    {
        new \Debug();
    }
    
    /**
     * LIST OF FILES
     *   $list = $service->files->listFiles();
     */
    public function listOfFiles()
    {
        new \Debug();
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 2015-02-06
      • 2022-01-03
      • 2018-10-26
      • 2021-09-22
      相关资源
      最近更新 更多