Wenn Sie versuchen, eine Datei mit window.fetch() herunterzuladen, sind die entsprechenden Schritte innerhalb des then-Blocks erforderlich wie folgt:
.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); });
Für eine präzisere und effizientere Lösung, die nur die Abruf-API verwendet, sollten Sie den folgenden Code in Betracht ziehen:
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); });
Das obige ist der detaillierte Inhalt vonWie lade ich eine Datei mit Window.fetch() herunter und handhabe die Autorisierung?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!