如何从网站内部下载整个网站

时间:2018-11-07 12:27:09

标签: javascript html

我正在创建一个网站,我想在其中向用户提供下载整个网站(包括CSS和图像)供他们修改的方法。我知道我可以使用以下方式下载单个资源

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

但是就像我说的那样,这只会下载一个文件,而我想下载整个网站。

如果它可以帮助您理解我的意思:在chrome,IE和Firefox中,您可以按ctrl + s下载整个网站(请确保将其另存为Web page, Complete

编辑:我知道我可以创建一个将下载的.zip文件,但是这样做需要我每次进行更改时都进行更新,而我不希望这样做。 ,因为我可能会进行很多更改。

1 个答案:

答案 0 :(得分: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.