gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)
'<p><img src="http://192.168.18.85/profile/upload/2023/07/17/oo20230717163925A001.png"…
- User
'oooooooppppppppoooippip'
- Assistant
- User
batch download file by use fetch in javascript
- Assistant
To batch download files using the
fetchAPI in JavaScript, you can loop through an array of file URLs and initiate multiple fetch requests. Here's an example:const fileUrls = [ "http://example.com/file1.pdf", "http://example.com/file2.txt", "http://example.com/file3.png" ]; async function downloadFiles() { const promises = fileUrls.map(url => fetch(url) .then(response => response.blob()) .then(blob => { // Extract filename from URL const filename = url.substring(url.lastIndexOf("/") + 1); // Create a temporary anchor element const a = document.createElement("a"); a.href = URL.createObjectURL(blob); a.download = filename; // Simulate click to trigger download a.click(); // Cleanup URL Object URL.revokeObjectURL(a.href); }) ); await Promise.all(promises); console.log("Files downloaded"); } downloadFiles();The
downloadFilesfunction uses thefetchAPI to retrieve the files asBlobobjects. It then creates temporary anchor elements (<a>) for each file, sets thehrefattribute to theBlobURL, and thedownloadattribute to the filename extracted from the URL. Finally, it triggers a simulated click on the anchor element to start the download.Note: This solution assumes the file URLs are accessible and the appropriate CORS headers are set on the server hosting the files.