2016-07-26 3 views
0

에서 xls을 다운로드 할 수 없습니다. 서버에서 생성 된 .xls 파일을 다운로드하고 싶습니다 .xls는 UI를 잠 그려면 생성하지만 Ajax 호출 파일은 다운로드되지 않습니다. 데이터 브라우저에서 제공하지만 보여 브라우저 대화 상자 "다른 이름으로 저장"하지 않았다 나는 Ajax 호출에 서버에서 생성 된 .XLS 파일을 다운로드 하려는Ajax Call of Jquery

1)

내 문제 문은, Jquery.

2) .xls 파일이 생성 될 때까지 나는 UI를 잠급니다.

jQuery의 파일을 다운로드 작동하지 (아약스 기능은을 .ajaxform).

submit() 함수는 파일을 성공적으로 다운로드하지만 파일 다운로드가 완료되었음을 알리는 기능이 없습니다.

주 : 1) '다른 이름으로 저장'옵션이 브라우저에서 사용 설정되었습니다.

2) .Xls file is getting generated from server side successfully 

을 제공하십시오

답변

0

가 아약스를 통해 수행하는 것이 불가능 사용할 수있는 다른 옵션의 경우. 성공 콜백과 같은 것을 할 수 있습니다.

window.location="download.xls/or/what/ever/?para1=value1...." 

그리고 적절한 다운로드 헤더를 서버에 설정하십시오.

0

불행하게도) 우리는 AJAX 요청

에서 파일을 다운로드 할 수 없습니다하지만 당신은 당신이 (다운로드 상태를 추적 유지하려는 생각

$("#id_of_form input[type='submit']").click(function(e){ 

// your code for lock screen here 

return true; 
}); 
0

벨로 코드로 화면 잠금을 할 수있는, U 쉽게 할 수 다운로드가 완료 될 때까지 UI를 잠그십시오. 불행히도 아약스 호출을 사용하여 다운로드 파일을 활성화하는 방법은 2 가지입니다.하지만 추가 html 요소를 사용하면 jquery-ajax 자체에서 다운로드를 활성화 할 수 있습니다. jquery가 다운로드를 완료 할 때까지 UI를 잠급니다.

방법 1

  1. 그 이름을 보내 XL (파일)을 생성하고 아약스와 응답의 모든 임시 위치로

  2. 콜이 방법을 해당 파일을 저장 뒤에 코드를 사용 파일을 저장하기 전에

  3. 태그를 jquery에 생성하고 click 이벤트를 활성화하면 파일을 다운로드합니다.

  4. 다운로드를 완료 한 후 u는 당신의 UI

    $.ajax({ 
    type: "POST", 
    url: AQ.AMT.Url + "/Home/StoreImage", 
    //async: false, 
    data: JSON.stringify({ request: request }), 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (Data) { 
    
        var _tempPPT = AQ.AMT.Url + "/Download/Temp/" + Data.Result + ".pptx"; 
        // Trick for making downloadable link 
        a = document.createElement('a'); 
        a.href = _tempPPT;// window.URL.createObjectURL(xhttp.response); 
        a.id = "idanchrPPT"; 
        // Give filename you wish to download 
        var today = new Date(); 
        var dd = today.getDate(); 
        var mm = today.getMonth() + 1; //January is 0! 
        var yyyy = today.getFullYear(); 
        if (dd < 10) { 
         dd = '0' + dd 
        } 
        if (mm < 10) { 
         mm = '0' + mm 
        } 
        today = dd + '/' + mm + '/' + yyyy; 
        if (flipName == "") 
         a.download = "" + name + "-" + today + ".pptx"; 
        else 
        a.download = "" + name + "-" + flipName + "-" + today + ".pptx"; 
        a.style.display = 'none'; 
        document.body.appendChild(a); 
        a.click(); 
        $("#idanchrPPT").remove(); 
    
    
        //deleting the file 
        var deleteFiles = {}; 
        deleteFiles.FilePath = Data.Result; 
        setTimeout(function() { 
         $.ajax({ 
          type: "Post", 
          data: JSON.stringify({ deleteFiles: deleteFiles }), 
          url: AQ.AMT.Url + "/Home/DeleteExportedPPt", 
          async: true, 
          contentType: "application/json; charset=utf-8", 
          dataType: "json", 
          success: function (result) { 
          }, 
          error: function (result) { 
          } 
         }); 
        }, 1000); 
        }, 
        error: function (e) { 
        } 
    }); 
    

방법 2

사용 XMLHttpRequest를 대신 jQuery를 $ 아약스의 잠금을 해제 할 수 있습니다

xhttp = new XMLHttpRequest(); 
xhttp.onreadystatechange = function() { 
    var a; 
    if (xhttp.readyState === 4 && xhttp.status === 200) { 

     var _tempPPT = AQ.AMT.Url + "/Download/Template/ExportPPtTrend1.pptx"; 

     // Trick for making downloadable link 
     a = document.createElement('a'); 
     a.href = _tempPPT;// window.URL.createObjectURL(xhttp.response); 
     a.id = "idanchrPPT"; 
     var k = xhttp.responseText; 
     // Give filename you wish to download 
     a.download = "test-file.pptx"; 
     a.style.display = 'none'; 
     document.body.appendChild(a); 

     a.click(); 
     $("#idanchrPPT").remove(); 

     //alert("ok"); 
     $('.divLoader,.divBackgroundCo').hide(); 
    } 
}; 
// Post data to URL which handles post request 
xhttp.open("POST", urlToHandler); 
//Send the proper header information along with the request 
xhttp.responseType = 'text'; 
//xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 


xhttp.send();