2017-11-20 6 views
1

서버 측에서 처리하도록 선택된 데이터 행 ('/ salarypayments/generateGirofile')을 보내려면 다음과 같은 기능을 가지고 있으며 파일을 다운로드하려면 클라이언트 측에 다시 URL을 반환합니다.여러 파일 다운로드

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

한 번에 여러 번 다운로드합니다.

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);

+0

파일을 압축하는 이외의 방법은 무엇입니까? 사용자를위한 추가 단계를 생성하지 않는 것이 좋습니다. – chris

+1

여러 파일에 대해 여러 앵커를 만들고 모든 앵커를 반복하여 클릭 할 수 있습니까? Theoractically이 작동해야합니다 –

+0

이 작업을 수행하는 방법에 대한 예제를 줄 수 있습니까? – chris