在点击文件下载成功重定向到使用AJAX另一个页面 [英] On click file download, on success redirect to another page using AJAX

查看:162
本文介绍了在点击文件下载成功重定向到使用AJAX另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Word preSS的项目,我的<大骨节病> 下载 包含的.zip 文件,它的onClick按钮应下载。因此,HTML产地是:

In my WordPress project, my Download button containing a .zip file, which onClick should be downloaded. So the HTML producing is:

<a id="732" class="btn btn-default download-link" href="https://example.com/download.zip">DOWNLOAD</a>

我使用AJAX刷新下载次数。

I'm using AJAX to refresh the download count.

<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
jQuery(document).on('click', '.download-link', function () {
    var id = this.id;

    jQuery.ajax({
        type: 'POST',
        url: ajaxurl,
        data: {"action": "count_download",
                    "id": id
                },
        success: function (data) {
            window.location = site.url + "/download-success?fid="+ id;
        }
    });
});
</script>

一切工作正常,直到我加入的链接文件。通常,这样的链接会开始下载的.zip 文件,但即使采取了AJAX的时间后调用重定向到下载成功页面的页面,而不会触发下载。

Everything works fine until I added the file with the link. Typically such a link will start downloading the .zip file, but even after the time taken by the AJAX call the page redirected to the download-success page without triggering the download.

和它发生的大部分时间,仅一次或两次的文件开始下载。

And it occurs most of the time, only once or twice the file starts downloading.

PS:我测试这个但它不是我的情况。

P.S.: I tested this but it's not my case.

推荐答案

同意@marc。只需添加更多的信息,做到这一点。

Agree with @marc. Just to add more information, do this.

<a href='download.php?file=some_file.zip'>Download</a>

以上,可以将网址链接,下载及以下将PHP code中的download.php

Above can be url link to download and below will be php code in download.php

//code to update download count (UPDATE tbl_dwn SET total = total + 1)
//below is code to download (hope you know it)
$zipName = $_GET['file']; //here you've to specify the absolute path to the download.zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=".$zipName."");
header("Content-length: " . filesize($zipName));
header("Pragma: no-cache"); 
header("Expires: 0");
readfile($zipName);

试试这个,让我知道你有任何问题。

Try out this and let me know if you've any problem.

这篇关于在点击文件下载成功重定向到使用AJAX另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆