首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个文件下载

多个文件下载
EN

Stack Overflow用户
提问于 2017-11-20 18:37:03
回答 1查看 45关注 0票数 1

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

代码语言:javascript
复制
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个文件。数组响应具有以下属性:

代码语言:javascript
复制
{ 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'}
EN

回答 1

Stack Overflow用户

发布于 2017-11-20 18:45:21

一次多次下载;

代码语言:javascript
复制
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 );

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47390015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档