如何添加下载功能

时间:2013-10-29 06:46:58

标签: asp.net c#-4.0

我在我的应用程序中添加了导出功能。它的工作非常好。现在我想在完成导出功能后添加自动下载功能。 使用c#asp.net

2 个答案:

答案 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();

    }

答案 1 :(得分: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']);
?>