Chat Dp
Chat Dp

Reputation: 627

Download Using Google Drive API with the original filename and extension

As the Google Drive API document said about downloading a file:

https://www.googleapis.com/drive/v3/files/FILEID?alt=media%26key=API_KEY

If we paste this link to Chrome or Safari It will start downloading.

But the problem is

For example

I have file mywork.fbx I upload to google drive, and the FileID is ABCDEFG

Then I go to the link below.

https://www.googleapis.com/drive/v3/files/ABCDEFG?alt=media%26key=MYAPIKEY

I got the file that name is ABCDEFG without extension

It should be mywork.fbx or anyname.fbx not just ABCDEFG

Tried

$file_url = 'https://www.googleapis.com/drive/v3/files/ABCDEFG?alt=media&key=KEY';
header('Content-Disposition: attachment; filename="mywork.fbx"');
readfile($file_url);

Upvotes: 4

Views: 3328

Answers (2)

pinoyyid
pinoyyid

Reputation: 22316

Look at the originalFilename property of https://developers.google.com/drive/api/v3/reference/files. You can use that in a content disposition header, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

Upvotes: 0

Tanaike
Tanaike

Reputation: 201643

  • You want to download a file which is not Google Docs using the browser.
  • You want to download a file with the filename which has the filename and extension of the original file.
  • You are using the API key.
    • In this case, the file is publicly shared.

If my understanding is correct, how about this answer? In this answer, webContentLink is used as the endpoint. The official document says as follows.

webContentLink:

A link for downloading the content of the file in a browser using cookie based authentication. In cases where the content is shared publicly, the content can be downloaded without any credentials.

Modified endpoint:

Please try to access the following endpoint with your browser.

https://drive.google.com/uc?export=download&id={fileId}
  • When you use this, please set the file ID. In this case, the file is not Google Docs.
  • When it accesses to the above endpoint, the file with the filename can be retrieved.

Reference:

If I misunderstood your question and this was not the direction, I apologize.

Upvotes: 2

Related Questions