JavaScriptArray
JavaScriptArray

Reputation: 140

create a folder with JavaScript

I know that JavaScript is not allowed by browsers to do much directly with the computer, and that is not my question. My question is: How to, with JavaScript, create a file on the web and then ask, perhaps with a popup window, to download the folder and any files within. This folder would need to allow access for JavaScript to place files within, like image files. This folder might originally be just another JavaScript value (like an object) or a URL.

I have done research, but much of what I found was asking whether JavaScript could create a folder directly to the computer, while my question is asking about creating the folder, and then asking to download it. There must be some way, because when in Google Drive, drive has a way to place multiple files in a .zip folder for download, and this works not just with Chrome but with Firefox and Internet Explorer. Some sites like http://stuk.github.io/jszip/ provide their own way to create .zip files, but I would prefer not to use a JavaScript library, if possible.

This would be useful for generating multiple image files, but only asking the user once for downloading them, instead of for each file.

I would prefer to create the zips client-side.

Upvotes: 2

Views: 7426

Answers (1)

JBux
JBux

Reputation: 1394

So this is an interesting question and I shall do my best to answer it. The simple solution is to not do this at all. Use javascript to asynchronously request the server to create the required .zip and then give the user a link/prompt to download. But I digress, you want it all in-house!

JSzip Option

You'll not be able to write a solid library that can .zip files client-side. JSzip is the best option out there, and it's not as solid as it could be. If you have the time you could go through the source and make improvements where required.

Cheeky Option

Your best option - baring the actual best one (server side) which you have discounted - is explained here. That is - create an iframe for each file.

Unfortunately you'll not find any better than those options if you're insisting on client-side work. Browsers weren't made to be zipping files.

Upvotes: 2

Related Questions