Rumah >hujung hadapan web >tutorial js >Bagaimana untuk Muat Turun Fail dengan Window.fetch() dan Mengendalikan Keizinan?
Apabila cuba memuat turun fail menggunakan window.fetch(), langkah yang sesuai untuk diambil dalam blok itu ialah seperti berikut:
.then((response) => { // Convert the response to a blob. return response.blob(); }) .then((blob) => { // Create a URL for the downloaded file. const fileURL = URL.createObjectURL(blob); // Navigate to the file URL to start downloading the file. window.location.assign(fileURL); });
Untuk penyelesaian yang lebih ringkas dan cekap hanya menggunakan API pengambilan, pertimbangkan kod berikut:
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); });
Atas ialah kandungan terperinci Bagaimana untuk Muat Turun Fail dengan Window.fetch() dan Mengendalikan Keizinan?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!