2013-04-11 37 views
1

我需要從JavaScript代碼的ASP.NET頁面啓動文件下載。從C#/ ASP.NET頁面的Javascript調用客戶端開始文件下載?

什麼是我最好的選擇?是否有可能調用Web服務來做到這一點,或者以某種方式調用C#頁面的常用事件處理程序?

請注意,我將檢索大量的數據,數兆字節。

+0

看到這個職位:http://stackoverflow.com/questions/873207/force-download-of-a -file上的Web服務器,ASP淨升c – Niels 2013-04-11 16:17:48

回答

2

您可以使用一個隱藏的IFRAME元素並啓動一個文件下載請求,該劑量給出了下載AJAX文件的感覺。

在下載文件時,您可以在客戶端的表單中執行其他活動。

是的,你可以調用web服務或ASPX頁面或HTTP處理程序,以及在此URL

function dowloadFileJS() { 
     // Create an IFRAME. 
     var iframe = document.createElement("iframe"); 

     // Point the IFRAME to GenerateFile 
     iframe.src = "GenerateFile.aspx?yourQueryString=myQueryString"; 

     // This makes the IFRAME invisible to the user. 
     iframe.style.display = "none"; 

     // Add the IFRAME to the page. This will trigger a request to GenerateFile now. 
     document.body.appendChild(iframe); 
    } 
相關問題