Warm tip: This article is reproduced from serverfault.com, please click

How to create file into Google Drive with post request in angular?

发布于 2020-11-28 21:53:00

I am trying to create a file in angular but I can't set the parameter like for the example name

@Injectable({
  providedIn: 'root'
})
export class DriveService {

  constructor(private http: HttpClient) {
  }

  token: string = localStorage.getItem('tokenGoogle');

  headers = new HttpHeaders()
    .set('Authorization', `Bearer ${this.token}`)
    .set('Accept', `application/json`)
    .set('Content-Type', `application/json`);

  creaFileDrive(nome: string): Observable<DriveResultFile> {

    return this.http.post<DriveResultFile>('https://www.googleapis.com/upload/drive/v3/files?key=AIzXXXXXXXXXXXXXXXXXX&uploadType=multipart',
      {name: 'pata'}, // HERE THE NAME DON'T SET
      {headers: this.headers}).pipe(
      map(x => {
        return x;
      })
    );
  }
}

clearly, this is not my goal

this is the result

thanks

Questioner
Edoardo
Viewed
0
Kavinda Senarathne 2020-11-29 08:20:32
  1. Use multipart upload https://developers.google.com/drive/v3/web/multipart-upload with different headers for file metadata and actual file. Me myself stuck there, didn't found how to add boundaries to separate request headers in Angular 2+

  2. Upload file in two requests. First to create empty file with metadata (response will provide id of the file) and second to actually "update" the file.

  3. Use resumable upload. First request to "setup metadata" (will not even create empty file) and get "special link" where to send request to upload actual file. And this approach have some other features, like uploading in chunks.https://developers.google.com/drive/v3/web/resumable-upload

Here is the link to another Question with implementation of resumable upload in Angular 2 and DRIVE REST API V3

Angular 2+ HTTP POST and GDrive API. Resumable file upload with name