2016-03-29 32 views
0

POSTデータのREST APIを使用しています。私はzipファイルを作成して強制ダウンロードを行っています。そのAPIを呼び出す必要があるのは、JCLeryを使用して必要なPOSTデータを送信する必要があるため、ダウンロードする必要があります。どうすればこれを達成できますか?JQueryを使用してzipファイルを強制ダウンロードする方法

答えて

0

あなたはこれを行うことができます:

HTML:

<input type="button" class="list" value="Download ZipFile" id="download"/> 

JS:

$("#download").click(function(){ 
    //this is the data when you send your request 
    var data = { yourKey1 :'something1', yourKey2:'something2'}; 

    // this is a fake response that we are assumming you got from ajax's response inside success call back 
    var response = {fileName:'yourFileName.zip' ,filePath : 'YouFilePath'}; 

    $.ajax({ 
     type: "POST", 
     url: 'yourURL', 
     data: data, 
     success: function(response) { 
      download(response); 
     } 
    }); 

}); 

var download = function (data){ 
    var link = document.createElement('a'); 
    link.href = data.filePath + data.fileName ; 
    //below you can define a new name 
    link.download = data.fileName; 
    document.body.appendChild(link); 
    link.click(); 
} 

例:https://jsfiddle.net/3yjt4Lah/8/

関連する問題