WebTorrent Workshop

06 - Create a download link for each file in the torrent

Write a script that downloads the torrent The WIRED CD and displays a download link for each file in the torrent.

Tips

Use the file.getBlobURL() function to get a URL which can be used to link to the file in the torrent. You can use this as the href in a link that you add to the page.

For each file in the torrent, run the following code:

file.getBlobURL((err, url) => {
  // If there was an error, print it in the console
  if (err) throw err

  // Create a link element
  const a = document.createElement('a')
  a.href = url
  a.textContent = 'Download ' + file.name
  a.style.display = 'block'

  // Download the file with given name when clicked
  a.download = file.name

  // Add the link to the page
  document.body.appendChild(a)
})

To learn more about the getBlobURL API, read the docs.

Verify

If it worked, you should see a list of links, one for each file in The WIRED CD torrent.

If you are stuck, read the solution.

When you are ready, go to the next exercise.