【问题标题】:How to make a popup display a message and then present a download after 5 seconds and then close the popup如何使弹出窗口显示一条消息,然后在 5 秒后显示下载,然后关闭弹出窗口
【发布时间】:2011-12-10 20:09:10
【问题描述】:

在我的项目中,我有一个希望用户下载的文件。当他们点击链接时,我想要一个弹出窗口显示“您的下载将很快,如果它没有开始点击这里”。几秒钟后,它会关闭并显示实际的文件下载。

我知道要实现你将使用的窗口关闭:

window.setTimeout(function(){window.close()}, 5000);

但我不确定一旦窗口关闭,您将如何调用下载?

为任何帮助干杯!

【问题讨论】:

  • 仅供参考:搜索“toast”将帮助您找到一些有用的东西
  • 一种方法是注入一个 iframe,其中 src = 使用 javacript 推送字节的文件/脚本文件的位置。不过,我不喜欢您的弹出窗口方法……为什么不直接重定向到一个页面,上面写着“您的下载将在 5 秒内开始,否则单击链接”。这是久经考验的。
  • 你在用jquery吗?如果是这样,我会为你敲代码
  • @Paul 我正在使用 JQuery 和 PHP(更准确地说是 Symfony2)

标签: javascript html javascript-events


【解决方案1】:

简单来说,使用window.open() 开始下载文件。

<a href="myfile.doc" id="download">Direct link</a>

<script type="text/javascript">
    setTimeout(function() {
        window.open("myfile.doc");
    },3000);
</script>

【讨论】:

    【解决方案2】:

    好的,所以我不知道你的平台,所以会给一个 ASP.NET 版本。如果您正在使用其他东西,那么我已经发表评论,因此您应该能够适应您的平台。

    编辑:现在知道用户正在使用 PHP,因此为 PHP 添加了代码 sn-p(不强大,但我不是 PHP 开发人员)...

    1) PHP/ASP 也一样 你得到一个浏览器不会自动显示的文件吗?即 .js 将按原样显示,但 exe 可能会触发文件下载对话框(如果有错误请有人纠正我,我会更新)

    如果您的文件总是会是 .exe,那么您可能会侥幸逃脱:

    $("body").append("<iframe src='http://www.targetsite.com/files/thefilename.exe'></iframe>");
    

    但您更有可能使用参数来查找正确的文件(并隐藏直接下载

    $("body").append("<iframe src='http://www.targetsite.com/downloader/?file=1234-1234-1234'></iframe>");
    

    在某些 setTimeout 函数中。

    如果文件类型未知,那么我建议将上述代码指向将文件字节流写入 http 响应的脚本文件(.ashx、php 等)。

    对于 PHP:

    <?php    // Demo - send a (binary) file
    
    $file = "ireland.jpg";//here you would use the query string parameter of the above
                          //ajax/iframe request eg file=1234-1234-1234 to find image in db
    $fp = fopen($file,"r") ;
    
    header("Content-Type: image/jpeg");//this would need to be modified to either show right content type or you could
                                       //set it to Application/force-download
    
    while (! feof($fp)) {
           $buff = fread($fp,4096);
           print $buff;
           }
    ?>
    

    警告请小心上面的代码。我突然想到你可能会直接传入文件名,我很确定有人可以使用它在你的应用程序的其他地方获取文件而无需仔细注意

    对于 ASP:

    我已经包含了一个示例 ashx(通用处理程序)解决方案:

    aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Wardle.PdfGenerator;
    using System.IO;
    
    public partial class user_account_iframedownloader : System.Web.UI.Page
    {
        private IInformixRepository _rep;
        //this page gets loaded into an iframe so we can do downloads while using ajax
        protected void Page_Load(object sender, EventArgs e)
        {
             //write bytes out here i.e. see after for methods
    
        }
    }
    

    示例字节输出方法(您只需要执行 File.getBytes 或其他操作 - 我的代码非常复杂,因此“为读者练习”

    public static void PdfOutput(byte[] pdfData, string filename)
    {
        HttpContext.Current.Response.ContentType = "Application/pdf";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename);
        HttpContext.Current.Response.BinaryWrite(pdfData);
    }
    public static void PdfZipOutput(byte[] zipData, string filename)
    {
        HttpContext.Current.Response.ContentType = "Application/zip";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename);
        HttpContext.Current.Response.BinaryWrite(zipData);
    }
    

    【讨论】:

    • 您可能希望使用 .ContentType ="application/force-download" 强制下载;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多