多个文件下载

时间:2017-11-20 10:37:03

标签: javascript download

我有以下功能来发送选择在服务器端处理的数据行(' / salarypayments / generateGirofile')并将url(s)返回到客户端下载文件:

genGiro: function(model) {
        var self = this;
        var controller = this.get('controller');

        var chosenPayslips = [];

        controller.get('chosenPayslips').forEach(function(chosenPayslip) {
            chosenPayslips.push(chosenPayslip.get('id'));
        });

        this.get('authObject').authorize('authorizer:application', (headerName, headerValue) => {
            const requestHeaders = {};
            requestHeaders[headerName] = headerValue;

            Ember.$.ajax({
                type: "POST",
                headers: requestHeaders,
                data:{
                    payslipsArray: chosenPayslips,
                    amountPaid: controller.get('amountPaid'),
                    service_type: controller.get('service_type'),
                    process_mode: controller.get('process_mode'),
                    valueDate: moment(controller.get('valueDate')).format('YYYYMMDD'),
                    countTran: 6,
                    user: this.get('authObject.session.content.authenticated.user.id')
                },
                url: this.store.adapterFor('application').get('namespace') + '/salarypayments/generateGirofile',
                success: function(response){

                    var link = document.createElement("a");
                    link.style.display = 'none';

                    document.body.appendChild( link );

                    response.forEach(function(download){

                        link.href = download.link;
                        link.download = download.filename;

                        link.click();
                    });

                    document.body.removeChild( link );

                    $('#myModal').modal('hide');
                    location.reload();

                },
                error: function(xhr, status, error){
                    console.log('Error ' + error);
                }
            });

        });
    }

但是,我只能启动1个文件的下载,它假设有2个文件。数组响应具有以下属性:

{ link: 'http://127.0.0.1/folder/1/file1.txt', filename: 'file1.txt'}
{ link: 'http://127.0.0.1/folder/1/file2.txt', filename: 'file2.txt'}

1 个答案:

答案 0 :(得分:-1)

一次多次下载;

var link = document.createElement("a");

response = [{
  "href" : "https://assets.babycenter.com/ims/2016/09/iStock_83513033_4x3.jpg",
  "name" : "baby-1.jpg"
},{
  "href" : "https://i.pinimg.com/736x/ea/97/16/ea97165480012b28ca1190e886239a0c--baby-costumes-photographing-babies.jpg",
  "name" : "baby-2.jpg"
},{
  "href" : "https://i.pinimg.com/736x/38/53/bf/3853bf5660dbb7abf589cee6d9060ccb--adorable-babies-cute-kids.jpg",
  "name" : "baby-3.jpg"
}]

link.setAttribute('download', null);
link.style.display = 'none';
document.body.appendChild( link );
response.forEach(function(download){
    link.setAttribute( 'href', download.href );
    link.setAttribute( 'download', download.name );
    link.click();
});
document.body.removeChild( link );

相关问题