在我的 WordPress 项目中,我的 下载 按钮包含一个 .zip
文件,应在 onClick 上下载该文件。所以生成的 HTML 是:
<a id="732" class="btn btn-default download-link" href="https://example.com/download.zip">DOWNLOAD</a>
我正在使用 AJAX 刷新下载计数。
<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 调用所花费的时间之后,页面也会重定向到下载成功页面,而不会触发下载。
而且这种情况大多数时候都会发生,只有一两次文件开始下载。
附注:我测试过 this但这不是我的情况。
最佳答案
同意@marc。只是为了添加更多信息,请执行此操作。
<a href='download.php?file=some_file.zip'>Download</a>
上面可以是下载的url链接,下面是download.php中的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);
试试这个,如果有任何问题请告诉我。
关于javascript - 单击文件下载后,成功后使用 AJAX 重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27927820/