Button to download file and redirect to new page

题意:下载文件并跳转到新页面的按钮

问题背景:

How do I enable my button to download and redirect to a new page? I have tried many code combination but it just downloads and does not redirect

如何使我的按钮能够下载文件并跳转到新页面?我已经尝试了很多代码组合,但它只是下载文件而不进行跳转。

<div class="banner">
    <h3>Download your file:<br> {{ filename }}</h3>
    <div>
        {% csrf_token %}
        <form action="{% url 'download_pdf' %}" enctype="multipart/form-data" method="post">
            {% csrf_token %}
            <button onclick="openTab()" class="btn btn-primary w-20" id="myBtn" tabindex="4">Download PDF
            </button>
        </form>
    </div>
</div>
<script>
function openTab() {
window.open('home');
}
</script>

问题解决:

You could try something like this to ensure both downloading the file and redirecting to a new page are achieved. The button click that already triggers download and then you can register a brief delay using JavaScript to redirect to your desired page:

你可以尝试这样做来确保既下载文件又跳转到新页面。按钮点击会触发下载,然后你可以使用JavaScript注册一个短暂的延迟来跳转到你的目标页面:

<div class="banner">
    <h3>Download your file:<br> {{ filename }}</h3>
    <div>
        {% csrf_token %}
        <form id="downloadForm" action="{% url 'download_pdf' %}" enctype="multipart/form-data" method="post">
            {% csrf_token %}
            <button type="button" class="btn btn-primary w-20" id="myBtn" tabindex="4" onclick="downloadAndRedirect()">Download PDF</button>
        </form>
    </div>
</div>

<script>
function downloadAndRedirect() {
    // Submit the form to trigger the download
    document.getElementById('downloadForm').submit();
    
    // Redirect after a short delay to allow the download to start
    setTimeout(function() {
        window.location.href = 'home'; // Change 'home' to your desired redirect URL
    }, 1000); // Adjust the delay time as necessary
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值