When it comes to downloading files using the window.fetch() API, you'll need to chain a then() block to the fetch() call to handle the response. Here's how you can do it:
<code class="javascript">function downloadFile(token, fileId) { let url = `https://www.googleapis.com/drive/v2/files/${fileId}?alt=media`; return fetch(url, { method: 'GET', headers: { 'Authorization': token } }).then(res => { // Handle the response here }); }</code>
In the then() block, you can typically use the following steps to download the file:
Here's a shorter and more efficient alternative that uses only the fetch API:
<code class="javascript">const url ='http://sample.example.file.doc' const authHeader ="Bearer 6Q************" const options = { headers: { Authorization: authHeader } }; fetch(url, options) .then(res => res.blob()) .then(blob => { var file = window.URL.createObjectURL(blob); window.location.assign(file); });</code>
The above is the detailed content of How to Download Files Using window.fetch()?. For more information, please follow other related articles on the PHP Chinese website!