从其他服务器下载之前是否可以动态压缩文件? [英] Is it possible to dynamicly zip files before downloading from another servers?

查看:28
本文介绍了从其他服务器下载之前是否可以动态压缩文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以编写浏览器扩展程序或简单的 Java Script 代码,其中包含指向不同位置的 URL 列表,可以将所有内容压缩在一起.

I am wondering is it possible to write browser extension or simple Java Script code, which having a list of URLs to different location, could zip everything together.

我的意思是...

例如...有 3 个不同的图像文件:

For instance...having 3 different image files on:

是否可以通过 http://mysite.com<上的 Java Script 代码将所有这些下载为压缩文件存档 images.zip/a> ?

Is it possible to download all of them as zipped files archive images.zip by Java Script code on http://mysite.com ?

我找到了 JSZip,但是我不知道如何使用它从不同的服务器加载文件.

I have found JSZip, however I don't know how use it to load files from different servers.

推荐答案

看看这段代码:我已经组装了一个 演示 让您从那里开始.向下滚动到代码的最后几行.

take a look at this code: I have assembled a Demo for you to get started from there. Scroll down to the last lines of the code.

首先我们需要将图像加载到 标签中.然后我们将阅读它们的内容.那么一切都可以通过jszip完成.

First we need to laod the images into <img> tags. then we will read the content of them. then all can be done via the jszip.

但是有限制.您不能从其他域加载图像.所以图像应该在同一个 url 中,或者你通过服务器端动态加载它们,比如:http://yourdomain.com/fileLoader.php?url=Url_To_External_Image

but there are limitations. you can not load images from another domains. so images should be in the same url or you dynamically load them via server side like: http://yourdomain.com/fileLoader.php?url=Url_To_External_Image

这是发挥魔力的代码狙击手:

here is the code snipee that do the magic:

function getBase64Image(imgage) {
    var canvas = document.createElement("canvas");
    canvas.width = imgage.width;
    canvas.height = imgage.height;

    var ctx = canvas.getContext("2d");
    ctx.drawImage(imgage, 0, 0);

    var dataURL = canvas.toDataURL("image/png");
    return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}

var img = $("<img />").attr('src', '../../../img/logo.png');

img.load(function() {
    if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
        alert('broken image!');
    }
    else {
        var zip = new JSZip();
        content = getBase64Image(img[0]);
        zip.file("24.jpg", content + "\n");
        var content = zip.generate();
        location.href="data:application/zip;base64,"+content;
    }
});

这篇关于从其他服务器下载之前是否可以动态压缩文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆