Ask coding questions

← Back to all posts
How to download a folder onto a user's device in javascript
Dunce (65)

I know you can download a file onto a user's device, like this:

let file = new File(["Example"], "example.txt", {
  type: "text/plain"
let url = URL.createObjectURL(file)

document.body.innerHTML = `
<a href="${url}" download="example.txt"> Download </a>

But how would I download a folder onto a user's device using a similar method? I've tried:

document.body.innerHTML = `
<a href="${url}" download="folder\\example.txt"> Download </a>

But it just replaces the backslash with an underscore in the filename, instead of creating a new folder. I've also tried it with a forward slash, but that didn't work either.
I haven't been able to find any information about this.

Coder100 (18139)

You can't download folders, but you can download zips.

Dunce (65)

@Coder100 Okay, how do I do that then?

JBloves27 (1884)

Hiya, not sure if this will help you, but here

Hope this helps!

Dunce (65)

@JBloves27 Not exactly what I was looking for. But thanks! :)