下载javascript之前,多个下载链接到一个zip文件 [英] Multiple download links to one zip file before download javascript

查看:430
本文介绍了下载javascript之前,多个下载链接到一个zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在javascript中将多个下载网址发送到一个zip文件中,并且可以下载该zip文件。很简单,在我的网页上,有一个按钮,当点击下载压缩到zip的所有文件的压缩文件的zip文件?

Is it possible, in javascript, to have multiple download urls sent into one zip file and that zip file can be downloaded. So pretty much, on my web page, there is one button, that when clicked downloads a zip file of all the files from the download urls compressed into the zip?

我相信我需要使用jszip或者这样的工具。这是一切可能的,有什么建议从哪里开始?

I believe I'd need to use jszip or some tool like that. Is this at all possible and is there any advice on where to start?

推荐答案

我最近不得不解决同样的问题,使用 JSZipUtils 找到解决方案。

I recently had to solve the same issue and had found a solution using JSZipUtils.

该解决方案可以在这里找到 http: //plnkr.co/edit/vWARo0dXbkgzmjyoRNi0?p=preview

The solution can be found here http://plnkr.co/edit/vWARo0dXbkgzmjyoRNi0?p=preview

我有两个文件,我想通过用户浏览器压缩和下载,我在两个文件上调用函数 getBinaryContent(path,callback)。这里的路径是存储文件的位置。

I have two files that I would like to zip and download via the users browser and I call the function getBinaryContent(path, callback) on both files. The path here is the where the file is stored.

这两个文件是一个.wav文件和一个.json文件。每个都应该被不同的对待,因此你应该使用.wav文件和 {binary:true的 {base64:true,binary:true} } 作为 JSZip 的函数文件的参数的json文件。

The two files are a .wav file and a .json file. Each of them should be treated differenly and hence you should use {base64:true,binary:true} for the .wav file and {binary:true} for the json file as an argument for the function file of JSZip.

代码也可以在这里找到

var file_confirmation=[false,false];
var file_name=["test1.wav","test.json"];
var urlList =["test.wav","test.json"];

var filenameSave ="myZip";


function zipFiles(id,urls)
{
    zip = new JSZip();

JSZipUtils.getBinaryContent(urls[0],function (err, data) 
{
    if(!err)
    {
        var dic={base64:true,binary:true}; //WAV File Encoding
        zip.file(file_name[0], data, dic);
        file_confirmation[0]=true;
        downloadZipIfAllReady(id);
    }
});

JSZipUtils.getBinaryContent(urls[1],function (err, data) 
    {
        if(!err)
        {
            var dic={binary:true}; //JSON File Encoding
            zip.file(file_name[1], data, dic);
            file_confirmation[1]=true;
            downloadZipIfAllReady(id);
        }
    });
}    

function downloadZipIfAllReady(id)
    {
        if(file_confirmation.every(function(element, index, array) {return element;}))
        {
             zip.generateAsync({type:"blob"})
                .then(function(content)
                {
                  var a = document.querySelector("#"+id);
                  a.download = filenameSave;
                  a.href = URL.createObjectURL(content);
                  a.click();
            });
    }
}


$(document).ready(function()
{

 zipFiles("a_id",urlList);

})

这篇关于下载javascript之前,多个下载链接到一个zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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