简体   繁体   English

如何使弹出窗口显示一条消息,然后在5秒钟后显示下载内容,然后关闭弹出窗口

[英]How to make a popup display a message and then present a download after 5 seconds and then close the popup

In my project, I have a file that I want the user to download. 在我的项目中,我有一个文件,希望用户下载。 When they click on the link, I want a popup window to display "Your download will shortly, if it doesn't start click here". 当他们单击链接时,我希望弹出窗口显示“您的下载将很快,如果它没有开始,请单击此处”。 After a few seconds, it will then close and the actual file download will then display. 几秒钟后,它将关闭,然后将显示实​​际文件下载。

I know to achieve the window closing you'll use: 我知道要实现窗口关闭,您将使用:

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

But I'm not sure how you would call the download once the window has closed? 但是我不确定在关闭窗口后如何调用下载?

Cheers for any help! 为任何帮助加油!

Okay so I don't know your platform so will give an ASP.NET version. 好的,所以我不知道您的平台,所以将提供ASP.NET版本。 If you are using something else then I have commented so you should be able to adapt to your platform. 如果您正在使用其他产品,那么我已发表评论,因此您应该能够适应您的平台。

EDIT: Now know user is using PHP so added code snippet for PHP (not robust but I'm no PHP dev)... 编辑:现在知道用户正在使用PHP,所以为PHP添加了代码段(不健壮,但我不是PHP开发人员)...

1) SAME FOR PHP/ASP Are you getting a file that will not be displayed by the browser automatically? 1)同样适用于PHP / ASP是否获得了不会由浏览器自动显示的文件? ie .js will be shown as is but an exe will probably trigger a file download dialog (someone correct me if wrong please and I'll update) 即.js将按原样显示,但exe可能会触发文件下载对话框(如果有误,请纠正我,我会更新)

If your files are always going to be ie .exe then you could probably just get away with: 如果您的文件始终是.exe,那么您可能就可以摆脱:

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

but more likely you will be using a parameter to find the right file (and hide direct download 但您更有可能会使用参数来找到正确的文件(并隐藏直接下载文件

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

in some setTimeout function. 在一些setTimeout函数中。

If the filetypes are unknown then I suggest pointing the above code at a script file (.ashx, php, etc) that writes the file byte stream to the http response. 如果文件类型未知,则建议将上述代码指向脚本文件(.ashx,php等),该脚本文件将文件字节流写入http响应。

FOR 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;
       }
?>

WARNING Be careful with the above code. 警告请谨慎使用以上代码。 It occurred to me you might pass in filename directly which I'm pretty sure someone could use to get files in other places in your app without careful attention 在我看来,您可能会直接传递文件名,我很确定有人可以在没有仔细关注的情况下使用它来获取您应用中其他位置的文件

FOR ASP: 对于ASP:

I have included an example ashx (generic handler) solution: 我提供了一个示例ashx(通用处理程序)解决方案:

aspx.cs 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

    }
}

Example byte output methods (you just need to do File.getBytes or something - my code is quite complicated so 'excercise for reader' 字节输出方法示例(您只需要执行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);
}

In simple way, use window.open() to start download file. 以简单的方式,使用window.open()开始下载文件。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM