0

I just need to read the content of a file(no need to download the file) and store the data into my application. I am able to get the content of a file in response by using this api "https://www.googleapis.com/drive/v3/files/fileId?alt=media". But the response of this api returns an unreadble format, something like this "�dҚ�ȳ:\u0010��$���2�m��L̆���✳ŝ-�lqg�;[����O�s�\u0011�\u001bk\".

Below is the code I am using. This is Gdrive api v3.

drive.files.get(
  {
    fileId: fileId,
    alt: "media",
  },
  (err, res) => {
    if (err) {
      console.log("err = ", err);
      return false;
    }

    if (res) {
      console.log("File Details");
      console.log(res);
    }
  }
);

How can I get the data in readable format?

Aditya yadav
  • 119
  • 1
  • 7
  • Does this answer your question? [Downloading a file from Google Drive in Javascript client](https://stackoverflow.com/questions/60839431/downloading-a-file-from-google-drive-in-javascript-client) – Linda Lawton - DaImTo Mar 15 '21 at 11:54
  • @DaImTo No. I am able to get the data(content of a file) in response but its not in readable format. So my question is how can I get the data in a readable(normal text) format. Thanks. – Aditya yadav Mar 16 '21 at 06:12
  • did you try downloading the file then opening it? it comes back as binary data not text your going to have to save it first then open the file. – Linda Lawton - DaImTo Mar 16 '21 at 11:56
  • What is the file type? As DalmTo says, you are downloading binary content which will not be readable. – iansedano Mar 16 '21 at 12:20
  • @DaImTo Is it not possible to get the content of a file in response itself rather than downloading the file. I am sorry if am asking a stupid question. I am asking this because I used the below api , it gave the actual content in response itself `drive.files.export( { fileId: fileId, mimeType: "text/plain", alt: "media", }, (err, res) => { if (err) { console.log("err = ", err); } if (res) { console.log(res.data); } } ); ` – Aditya yadav Mar 17 '21 at 06:52

1 Answers1

0

Based on your comment:

I used the below api , it gave the actual content in response itself

The key in that particular request is that the file mimeType is:

mimeType: "text/plain"

This means that the content of this file is just text, so you can read the contents no problem.

However, it looks like in your original question you are requesting a file that is not plain text. Maybe a PDF, or a Word .doc. These files are encoded in binary so their content is not readable by humans. To read the contents you need the appropriate program.

Though you haven't provided the full code, so I can't be sure what type of file you are trying to download.

iansedano
  • 6,169
  • 2
  • 12
  • 24