Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Upload Files to Google Drive using Javascript

Upload Files to Google Drive using Javascript

News

At October 11, 2019, I published a Javascript library to to run the resumable upload for Google Drive. When this is used, the large file can be uploaded. You can also use this js library.

Description

This is a sample script for uploading files to Google Drive using Javascript. The files are uploaded by Drive API v3. gapi.client.drive.files.create() can create an empty file on Google Drive. But it cannot directly upload files including contents. I think that this might not be able to upload files and metadata with the multipart/related, although this might be resolved by the future update. So now, as one of workarounds, I use using XMLHttpRequest.

  • This sample uses gapi.
    • Before you use this, please enable Drive API at API console and carry out the installation of gapi.
  • When this script is run, a text file including "sample text" is created to Google Drive.
  • When you use this script, please set fileContent and metadata.

In this sample script, a text file including contents is created under a folder.

Sample script :

var fileContent = 'sample text'; // As a sample, upload a text file.
var file = new Blob([fileContent], {type: 'text/plain'});
var metadata = {
    'name': 'sampleName', // Filename at Google Drive
    'mimeType': 'text/plain', // mimeType at Google Drive
    'parents': ['### folder ID ###'], // Folder ID at Google Drive
};

var accessToken = gapi.auth.getToken().access_token; // Here gapi is used for retrieving the access token.
var form = new FormData();
form.append('metadata', new Blob([JSON.stringify(metadata)], {type: 'application/json'}));
form.append('file', file);

var xhr = new XMLHttpRequest();
xhr.open('post', 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id');
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.responseType = 'json';
xhr.onload = () => {
    console.log(xhr.response.id); // Retrieve uploaded file ID.
};
xhr.send(form);

Request body :

In this script, form is as follows. This is sent to Google Drive using the create method of Drive API.

------WebKitFormBoundaryxX0XmxgooMjdUECR
Content-Disposition: form-data; name="metadata"; filename="blob"
Content-Type: application/json

{"name":"sampleName","mimeType":"text/plain","parents":["#####"]}
------WebKitFormBoundaryxX0XmxgooMjdUECR
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: text/plain

sample text
------WebKitFormBoundaryxX0XmxgooMjdUECR--

Sample script 2

When XMLHttpRequest is modified to fetch, the script becomes as follows.

var fileContent = 'sample text'; // As a sample, upload a text file.
var file = new Blob([fileContent], {type: 'text/plain'});
var metadata = {
    'name': 'sampleName', // Filename at Google Drive
    'mimeType': 'text/plain', // mimeType at Google Drive
    'parents': ['### folder ID ###'], // Folder ID at Google Drive
};

var accessToken = gapi.auth.getToken().access_token; // Here gapi is used for retrieving the access token.
var form = new FormData();
form.append('metadata', new Blob([JSON.stringify(metadata)], { type: 'application/json' }));
form.append('file', file);

fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id', {
    method: 'POST',
    headers: new Headers({ 'Authorization': 'Bearer ' + accessToken }),
    body: form,
}).then((res) => {
    return res.json();
}).then(function(val) {
    console.log(val);
});

Testing:

  • January 11, 2022: When I tested this script, no error occurs. I could confirm that A text file inlucing sample text as the filename of sampleName can be created to the specific folder.
@jamsshhayd
Copy link

jamsshhayd commented Oct 5, 2018

So, How the authorization of google account will work?

@spinualexandru
Copy link

spinualexandru commented Oct 19, 2018

So, How the authorization of google account will work?

Seems like authentication is done using the standard access token via gapi: Google API Reference

@gutierrezjeralds
Copy link

gutierrezjeralds commented Nov 16, 2018

Hello, how about the percentage of the upload progress?

@gsborisgithub
Copy link

gsborisgithub commented Dec 20, 2018

'parents': ['### folder ID ###']
do you need to provide it?

@dmarshall83
Copy link

dmarshall83 commented Jan 10, 2019

any idea how to get this to work with resumeable uploads

@SamuelMuloki
Copy link

SamuelMuloki commented Mar 22, 2019

This worked great for me thanks.

@tanaikech
Copy link
Author

tanaikech commented Apr 7, 2019

You can see a sample about the resumable upload at https://github.com/tanaikech/Resumable_Upload_For_WebApps

@tanaikech
Copy link
Author

tanaikech commented Apr 7, 2019

If you don't want to put the file to the specific folder, you can remove 'parents': ['### folder ID ###'],.

@tyzero
Copy link

tyzero commented Aug 9, 2019

Cool Man!

@tanaikech
Copy link
Author

tanaikech commented Aug 10, 2019

Thank you!

@trantronghien85
Copy link

trantronghien85 commented Apr 3, 2020

Hi tannaukech, how to upload a file from input element to google drive? I tried to use javascript object file to upload, but i got a problem. I attach a picture about my problem below. I'm looking forward to hear from you soon. Thank you.
image

@sarangnx
Copy link

sarangnx commented Apr 7, 2020

Seems like you are converting the form object to string, I guess.

@itsmhuang
Copy link

itsmhuang commented Apr 22, 2020

Was able to get it to work but what do I do with the file ID that's returned? It doesn't match any part of the URL for the created file when I go to google drive and open it. I assume I have to call another endpoint to get an actual URL.

@itsmhuang
Copy link

itsmhuang commented Apr 22, 2020

figured it out for anyone interested. Add to the endpoint the fields param with value webViewLink. This tutorial has fields=id, but use fields=webViewLink instead or add to it and whatever other properties you're interested in the response. E.g. 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=webViewLink'

@RicardoSimoesRKS
Copy link

RicardoSimoesRKS commented Sep 21, 2020

I tried out this code but I'm getting a
Exception: Unexpected error while getting the method or property newBlob on object Utilities. error.
I thought this script didn't use any google script, and I couldn't find anywhere a place to set my own script.
Its just giving me this one: https://script.google.com/macros/s/AKfycbwhJjevFmbIZ_FQztQKHV9s9yXwPy-v7WBlOuz4W8ejN7gNgVs/exec

@nwaughachukwuma
Copy link

nwaughachukwuma commented Oct 23, 2020

@tanaikech, please how do I get this to work on mobile. Currently, only on mobile, it uploads, returns a 200 and the file id, but I cannot find the file on Google Drive. However, it works fine on web without issues.

@nwaughachukwuma
Copy link

nwaughachukwuma commented Oct 25, 2020

Hi @tanaikech, I have a situation where this works on the web but not on mobile. On mobile, the upload mechanism doesn't seem to detect the metadata and as such, the file does not have a name/title. Please see this question on stack overflow

@aznoisib
Copy link

aznoisib commented Feb 24, 2021

just fields parent as 'root'

@nwaughachukwuma
Copy link

nwaughachukwuma commented Feb 25, 2021

Ok thanks @aznoisib but I couldn't it to work. To be clear, do you mean to construct the metadata field as below?

 const metadata = {
     name: file.name, 
     title: file.name,
     mimeType: contentType,
     // parents: ["parent-folder-id"],
     parents: ['root'], // <--- correct?
   };

@fullstackdev427
Copy link

fullstackdev427 commented Aug 31, 2021

How can I upload files into shared drive ?
Is this related to OAuth scope or User type (External or Internal) ?

@usamar98
Copy link

usamar98 commented Sep 10, 2021

var accessToken = gapi.auth.getToken().access_token;
i can get error in this line

@F46y
Copy link

F46y commented Dec 10, 2021

Thank you so much! After hours of trying around, this saved us!

@felixkrautschuk
Copy link

felixkrautschuk commented Jan 10, 2022

Is this guide still up to date? On our side it is always uploading a file with name "Untitled" and I have no idea why the file name is not set correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment