首页 > 解决方案 > 在Angular 4中将文件上传到Google驱动器中的特定文件夹

问题描述

我正在尝试将文件上传到特定文件夹,但我无法做到。上传工作正常,但它没有将文件放在特定文件夹中。

我正在尝试使用 google drive rest 版本 3 进行可恢复上传。

假设我已经有一个文件夹 ID。

首先获取上传 URI:

uploadFileToDrive(name: string, content: string): Promise<Object> {
        const url = `https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable`;
        const accessToken = localStorage.getItem('accessToken');
        let headers = new Headers({
            'Content-Type': 'application/json; charset=UTF-8',
            'Authorization': 'Bearer ' + accessToken,
        });
        let options = new RequestOptions({ headers: headers }); // Create a request option
        return this.http
            .post(url, { name: name, role: 'reader', type: 'anyone', 'parents': [{"id":parentId}] }, options)
            .toPromise()
            .then(response => this.gDriveUploadFile(content, response.headers.get('location')));
    }

第二次上传媒体:

gDriveUploadFile(file, url): Promise<any> { //file and url we got from first func
        console.log(file + " "+ url );
        const accessToken = localStorage.getItem('accessToken');
        let headers = new Headers({
            'Authorization': 'Bearer ' + accessToken,
            'Content-Type': 'application/json; charset=UTF-8',
            'X-Upload-Content-Type': file.type ,
        });

        let options = new RequestOptions({ headers: headers }); // Create a request option

        return this.http.post(`${url}`, file, options) //call proper resumable upload endpoint and pass just file as body
            .toPromise()
    }

另外,我想知道如何使用 google rest API 以 angular 创建一个文件夹。

标签: javascriptangulargoogle-drive-apiangular5

解决方案


推荐阅读