JQuery

2016-03-29 28 views
0

ile zip dosyasının zorla karşıdan yüklenmesiyle nasıl işlem yaparım POST verisiyle bir REST API'si kullanıyorum. Bir zip dosyası oluşturuyorum ve zorunlu bir indirme işlemi yapıyorum. Gerekli olan POST verilerini JQuery kullanarak göndererek bu API'yi çağırmalıyım. Bunu nasıl başarabilirim?JQuery

cevap

0

Bunu yapabilirsiniz:

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

Örnek:https://jsfiddle.net/3yjt4Lah/8/