1

I'm making a website, in which I want to offer the user to download the whole website (CSS and images included) for them to modify. I know I can download individual resources with

<a href="./resource.extention" download="filename">Click Me</a>

but like I said, this only downloads one file, whereas I would like to download the entire website.

If it helps you visualise what I mean: in chrome, IE and Firefox you can press ctrl+s to download the entire website (make sure you save it as Web page, Complete.

Edit: I know I can create a .zip file that it will download, however doing so requires me to update it every time I make a change, which is something I'd rather not do, as I could potentially be making a lot of changes.

Ramen Noodles
  • 211
  • 3
  • 8
  • I don't think it's possible. it would be better to have your URL integrated with some 3rd-party scrapper which offers to download the scrapped content (hopefully as a free service) or you can set-up your own server so whenever some code changes (commits pushed) it will generate some zip file of whatever files you want. – vsync Nov 07 '18 at 12:43
  • 1
    How about just putting a zip file on your site and letting people download it with the method you said? – rpm192 Nov 07 '18 at 12:44
  • linked - https://stackoverflow.com/q/12609121/104380 – vsync Nov 07 '18 at 12:44
  • In addition to @rpm192, you can add a [cron job](https://www.ostechnix.com/a-beginners-guide-to-cron-jobs/) that zips all the static files for you every predefined time so you won't need to do it manually each time you update your site. – jurl Nov 07 '18 at 14:30
  • @rpm192 I suppose I could do that, but it would require me to update the .zip file every time I made a change, which is something I'd rather not do. – Ramen Noodles Nov 07 '18 at 15:22
  • As @jurl said, create a cron job, so it updates it automatically every day or so. – rpm192 Nov 07 '18 at 15:23
  • @jurl I'm not sure if the people I'm using to host the site allow me to do cron jobs, but I'll look into it. – Ramen Noodles Nov 07 '18 at 15:26
  • 1
    You may also create a link, clicking on which it triggers a piece of code on server side, which gets the site id or something unique, gets all the updated resources for that particular website, creates a zip and disposes it. – Subhasish Bhattacharjee Nov 07 '18 at 15:27
  • If people can get the code for the site, then I guess it's open-source (or kind of)? If you're using source control, like git, you could put it up in github (or similar service), and link to the `master.zip` download file they provide, whenever you push your commits, that file is updated. (supposed you're using just a master branch) – Leite Nov 07 '18 at 15:28

1 Answers1

1

As I mention, it is better that you will have a cron job or something like this that once in a while will create you a zip file of all the desired static content.

If you insist doing it in javascript at the client side have a look at JSZip .

You still have to find a way to get the list of static files of the server to save. For instance, you can create a txt file with each line is a link to a webpage static file.

you will have to iterate over this file and use $.get to get it's content.

something like this:

// Get list of files to save (either by GET request or hardcoded)
filesList = ["f1.json /echo/jsonp?name=1", "inner/f2.json /echo/jsonp?name=2"];

function createZip() {
    zip = new JSZip();

    // make bunch of requests to get files content
    var requests = [];
    // for scoping the fileName
    _then = (fname) => data => ({ fileName: fname, data });
    for (var file of filesList) {
        [fileName, fileUrl] = file.split(" ");
        requests.push($.get(fileUrl).then(_then(fileName)));
    }

    // When all finished
    $.when(...requests).then(function () {
        // Add each result to the zip
        for (var arg of arguments) {
            zip.file(arg.fileName, JSON.stringify(arg.data));
        }

        // Save
        zip.generateAsync({ type: "blob" })
            .then(function (blob) {
                saveAs(blob, "site.zip");
            });
    });
}

$("#saver").click(() => {
    createZip();
});

JSFiddle

Personally, I don't like this approach. But do as you prefer.

jurl
  • 2,504
  • 1
  • 17
  • 20