0

I've done the Google Drive API integration. User can choose/upload file from google drive. Now, I want to save google drive file into my local directory.

I am getting binary data of file using below code (res.body) when I'm selecting file(s) from Google Drive.

I'm getting binary data of file in "res.body". How to save file into local directory using this binary data.

//====Called when a file has been selected in the Google Picker Dialog Box======
PickerResponse: function(data) {
  if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
    var file = data[google.picker.Response.DOCUMENTS][0],
      id = file[google.picker.Document.ID],
      request = gapi.client.drive.files.get({
        fileId: id
      });

    this.ShowPicker();
    request.execute(this.GetFileDetails.bind(this));

    gapi.client.drive.files.get({
      fileId: id,
      alt: "media"
    }).then(function(res) { **
      console.log(res.body) **
    });
  }
}
  • Is this on the server or the client? Server solution: [`fs.promises.writeFile`](https://nodejs.org/api/fs.html#fspromiseswritefilefile-data-options) - Client solution: [Creating and downloading files with JS](https://javascript.plainenglish.io/javascript-create-file-c36f8bccb3be). – CherryDT Sep 21 '22 at 08:17
  • @CherryDT `this.ShowPicker();`, so I'd assume client. – Cerbrus Sep 21 '22 at 08:18
  • It is client side javascript – Denis Patel Sep 22 '22 at 02:43
  • Thankyou @CherryDT for response. Below is your response for client side. Client solution: Creating and downloading files with JS But I need to save file (not download) or write data into file object using simple javascript. – Denis Patel Sep 22 '22 at 02:47
  • Do you mean "save, not download" as in "save without asking every time"? In this case you can use the [File System Access API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API) (not available in Firefox or Safari, only Chrome/Chromium-based browsers!). The user will have to select a directory and give you write access to it, then you can create files in that directory. Otherwise, it's not possible. - Or maybe I misunderstood what you mean by "save, not download", because it's pretty much the same thing in terms of client-side JavaScript, no? – CherryDT Sep 22 '22 at 11:28
  • Yes it's pretty much the same thing so I think I'm unable to explain you. I just simply want to save file which is chosen from google drive in local directory without asking user. Like whenever we need to upload profile picture anywhere then we select picture and it will save to the directory and when we need to display that image then we fetch that image from directory. – Denis Patel Sep 24 '22 at 07:51

0 Answers0