2

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.

JavaScriptArray
  • 140
  • 2
  • 11
  • 1
    It's possible with JavaScript alone, but it's relatively complex. Google makes the zips on the server which is the easy solution. – JJJ Jun 22 '15 at 21:24
  • @Juhana, this is good information you have commented. Even though it is complex, is there a source you would recommend to learn more about creating zips client-side? If you don't have one in mind, don't worry much about finding one. Thanks, again, for the response. – JavaScriptArray Jun 22 '15 at 21:30
  • possible duplicate of [Create a file in memory for user to download, not through server](http://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server) – Oliver Gondža Jun 22 '15 at 21:31
  • 1
    If you don't want to use a ready-made library for creating zips you'll have to make your own. You'll have to read up on how the zip compression works, implement the algorithm in JS, and look up on how to pass the created files to the user. But unless you specifically want to learn how to implement zip compression there's not much reason to re-invent the wheel. – JJJ Jun 22 '15 at 21:34
  • @Juhana, you're right. Perhaps I might use the library. :) – JavaScriptArray Jun 22 '15 at 21:35

1 Answers1

2

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.

Community
  • 1
  • 1
JBux
  • 1,394
  • 8
  • 17