本文介绍了下载文件并重定向...或替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户填写表格.使用有效的表单,他们单击以下载文件.我希望能够将它们重定向到另一个页面(例如,page2.html)并启动下载.

Users fill out a form. With a valid form, they click to download a file. I would like to be able to redirect them to another page (e.g., page2.html) AND initiate a download.

我无法在 page2.html 上启动下载,因为如果不直接点击下载链接,大多数浏览器都会标记安全警告.

I can't initiate download on page2.html because most browsers will flag security warnings if a download link isn't directly clicked on.

如何使用 javascript 或 jquery 完成这样的事情?显然,以下内容不起作用,但仅用于说明目的:

How is something like this accomplished using javascript or jquery? Obviously the following doesn't work but it's meant for illustrative purposes:

document.getElementById("foo").onclick = function(){
     window.location = "/download.exe";
     window.location = "/page2.html";
}

推荐答案

您可以使用文件链接和 onclick 来触发立即转到下一页".

You could use a link to the file and onclick to trigger "go to the next page shortly".

<script>
function thanks() {
    setTimeout(function () {
        document.location.pathname = "page2.html";
    }, 1000);
}
</script>

<a href="file.exe" onclick="thanks()">Download now!</a>

这篇关于下载文件并重定向...或替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:31