通过jquery下载文件并重定向

时间:2023-02-06 07:52:00

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在单击文本链接时触发下载。此外,单击后,用户将重定向到成功页面。有人可以帮忙吗?

1 个解决方案

#1


11  

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();
    }
);

#1


11  

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();
    }
);