下载文件并重定向...或替代方案

时间:2023-02-05 22:50:41

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.

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

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

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

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

2 个解决方案

#1


11  

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

你可以使用文件的链接和onclick来触发“很快转到下一页”。

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

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

#2


3  

You can initiate the download on page 2 by having page2.html "change" its location (to your download URL) once the page is loaded:

您可以通过在页面加载后让page2.html“更改”其位置(到您的下载URL)来启动第2页的下载:

$(function ()
{
    setTimeout(function ()
    {
        window.location.pathname = '/download.exe';
    }, 5000); // download the file in 5 seconds
});

I'm pretty sure (though not 100%) that won't trigger a security warning.

我很确定(虽然不是100%)不会触发安全警告。

#1


11  

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

你可以使用文件的链接和onclick来触发“很快转到下一页”。

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

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

#2


3  

You can initiate the download on page 2 by having page2.html "change" its location (to your download URL) once the page is loaded:

您可以通过在页面加载后让page2.html“更改”其位置(到您的下载URL)来启动第2页的下载:

$(function ()
{
    setTimeout(function ()
    {
        window.location.pathname = '/download.exe';
    }, 5000); // download the file in 5 seconds
});

I'm pretty sure (though not 100%) that won't trigger a security warning.

我很确定(虽然不是100%)不会触发安全警告。