Skip to content
The Chat Archive
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"…

  1. User

    'oooooooppppppppoooippip'

  2. Assistant
  3. User

    batch download file by use fetch in javascript

  4. Assistant

    To batch download files using the fetch API 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 downloadFiles function uses the fetch API to retrieve the files as Blob objects. It then creates temporary anchor elements (<a>) for each file, sets the href attribute to the Blob URL, and the download attribute 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.