If I want to download a file, what should I do in the then
block below?
JavaScript
x
10
10
1
function downloadFile(token, fileId) {
2
let url = `https://www.googleapis.com/drive/v2/files/${fileId}?alt=media`;
3
return fetch(url, {
4
method: 'GET',
5
headers: {
6
'Authorization': token
7
}
8
}).then( );
9
}
10
Note: The code is on the client-side.
Advertisement
Answer
I temporarily solve this problem by using download.js and blob
.
JavaScript
1
18
18
1
let download = require('./download.min');
2
3
4
5
function downloadFile(token, fileId) {
6
let url = `https://www.googleapis.com/drive/v2/files/${fileId}?alt=media`;
7
return fetch(url, {
8
method: 'GET',
9
headers: {
10
'Authorization': token
11
}
12
}).then(function(resp) {
13
return resp.blob();
14
}).then(function(blob) {
15
download(blob);
16
});
17
}
18
It’s working for small files, but maybe not working for large files. I think I should dig Stream more.