Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

React.js Google Drive File Picker API to Upload,Download Files to Drive in Browser Using Javascript

Posted on January 19, 2023

 

 

 

Welcome folks today in this blog post we will be using the google drive api to upload and download files to google drive using the drive picker in browser using javascript. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new react.js project using the below command

 

 

npx create-react-app sampleapp

 

 

npm i react-google-drive-picker

 

 

And after that you will see the below directory structure of the react.js app as shown below

 

 

 

 

And now you need to make an App.js file and copy paste the following code

 

 

App.js

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { useEffect } from "react";
import useDrivePicker from "react-google-drive-picker";
 
function App() {
  const [openPicker, data, authResponse] = useDrivePicker();
  // const customViewsArray = [new google.picker.DocsView()]; // custom view
  const handleOpenPicker = () => {
    openPicker({
      clientId:"##clientid##",
      developerKey:"##apikey##",
      viewId:"DOCS",
      token:"##accesstoken##", // pass oauth token in case you already have one
      showUploadView: true,
      showUploadFolders: true,
      supportDrives: true,
      multiselect: true,
      // customViews: customViewsArray, // custom view
    });
  };
 
  useEffect(() => {
    // do anything with the selected/uploaded files
    if (data) {
       console.log(data)
      data.docs.map((i) => console.log(i));
    }
  }, [data]);
 
  return (
    <div>
      <button onClick={() => handleOpenPicker()}>Open Picker</button>
    </div>
  );
}
 
export default App;

 

 

As you can see we are importing the react-google-drive-picker library and then we need to replace your own clientid and apikey and then we need to replace the accessToken and then we need to enable the google drive picker api.  And now we have the button called Open Picker when user clicked a popup window will appear where user can select accounts and then user can upload and download files to google drive as shown below

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Recent Posts

  • Build a JWT Login & Registration Auth System in Node.js & Express Using MongoDB in Browser
  • React-Admin Example to Create CRUD REST API Using JSON-Server Library in Browser Using Javascript
  • Javascript Papaparse Example to Parse CSV Files and Export to JSON File and Download it as Attachment
  • Javascript Select2.js Example to Display Single & Multi-Select Dropdown & Fetch Remote Data Using Ajax in Dropdown
  • Video.js Video Player Plugin Library in Javascript For Playing Videos in Browser
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme