在Angular 4中将文件上传到Google驱动器中的特定文件夹

时间:2018-09-29 08:54:08

标签: javascript angular google-drive-api angular5

我正在尝试将文件上传到特定的文件夹,但是无法执行。 上传工作正常,但是没有将文件放入特定的文件夹中。

我正在尝试使用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中创建文件夹。

0 个答案:

没有答案