0

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

4

2 回答 2

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']);
?>
于 2013-10-29T11:07:41.013 回答
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();

    }
于 2013-10-29T07:12:04.657 回答