2013-10-29 37 views
0

我在我的應用程序中添加了導出功能。它工作得很好。現在我想在完成導出功能後添加自動下載功能。 使用c#asp.net如何添加下載功能

+0

任何你試過的東西?任何代碼? –

回答

0

您可以通過以下方式實現此目的:string file =「」;

file = "Path of File Name to Download"; 
    if (file != "") 
    { 
     context.Response.Clear(); 
     context.Response.ContentType = "application/octet-stream"; 
     context.Response.AddHeader("content-disposition", "attachment;filename=" + Path.GetFileName(file)); 
     context.Response.WriteFile(file); 
     context.Response.End(); 

    } 
0

添加此腳本。

<script type="text/javascript"> 
function populateIframe(id,path) 
{ 
    var ifrm = document.getElementById(id); 
    ifrm.src = "download.php?path="+path; 
} 
</script> 

廣場這個地方你想要的下載按鈕(在這裏我們只使用一個鏈接):

<iframe id="frame1" style="display:none"></iframe> 
<a href="javascript:populateIframe('frame1','<?php echo $path; ?>')">download</a> 

文件「的download.php」(需要你的服務器上放)只包含:

<?php 
    header("Content-Type: application/octet-stream"); 
    header("Content-Disposition: attachment; filename=".$_GET['path']); 
    readfile($_GET['path']); 
?>