javascript 通过 jquery 下载文件并重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6894900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:15:00  来源:igfitidea点击:

Download a file via jquery and redirect

javascriptjqueryfileredirectdownload

提问by Matt Lambert

I'd like to figure out a simple way using jQuery to trigger a download when a text link is clicked. Also, after click the user is redirected to success page. Can anyone help?

我想找出一种使用 jQuery 在单击文本链接时触发下载的简单方法。此外,单击后,用户将被重定向到成功页面。任何人都可以帮忙吗?

回答by fsong

Let's say you have a link to your download page

假设您有一个指向下载页面的链接

<a class="downloadLink" href="www.example.com/foo.pdf">Download</a>

In your Javascript:

在您的 Javascript 中:

$(".downloadLink").click(
    function(e) {   
        e.preventDefault();

        //open download link in new page
        window.open( $(this).attr("href") );

        //redirect current page to success page
        window.location="www.example.com/success.html";
        window.focus();
    }
);