3

I just want to use Google Drive SDK in order to upload a simple file to my drive using JavaScript. The file is already been stored on my computer, all just I want is that read that file and then upload it to my drive.

Thanks in advance.

2 Answers 2

5
  • Here are explained all types of uploads you can do with Google Drive API.
  • My code does a multipart upload with a client-side request using v3 Google Drive API. You can do it server-side as well.
  • In the metadata object parents property can be used to upload file to a specific folder. It's not mandatory.
const fileToUpload = inputUpload.files[0];

var metadata = {
  name: fileToUpload.name,
  mimeType: fileToUpload.type
  //parents: ["folderID"]
};

var formData = new FormData();
formData.append( "metadata", new Blob( [JSON.stringify( metadata )], {type: "application/json"} ));
formData.append( "file", fileToUpload );

fetch( "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", {
  method: "POST",
  headers: new Headers({ "Authorization": "Bearer " + gapi.auth.getToken().access_token }),
  body: formData
}).then( function( response ){
  return response.json();
}).then( function( value ){
  console.log( value );
});

In order to make work this code, you have to be been authorized with OAuth 2.0.

  • Here you can find an example of authentication and here a better explanation on authentication. Both links are from APIs documentation.

Anyway a similar question had been already answered.

Sign up to request clarification or add additional context in comments.

Comments

-3

Here is quickstart of javascript in official documentation which covers exactly what you want.

4 Comments

no, that makes user able of uploading a file, what i want is that when a page is loaded a file automatically gets uploaded onto the drive. The file is on my local computer.
This does cover uploading a file bit it doesn't use the "simple" method but instead uses the multi-part method.
This link does not answer the user's question. It shows how to authenticate to drive and list files in the drive, not how to upload a file to the drive.
The link contains information on how to setup "a simple JavaScript application that makes requests to the Drive API" It doesn't contain JS code samples that perform upload API call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.