Javascript 单击文件下载,单击成功,使用AJAX重定向到另一个页面
在我的WordPress项目中,我的下载按钮包含一个Javascript 单击文件下载,单击成功,使用AJAX重定向到另一个页面,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,在我的WordPress项目中,我的下载按钮包含一个.zip文件,单击该文件应该下载。因此,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
.zip
文件,单击该文件应该下载。因此,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>
var ajaxurl='';
jQuery(文档).on('click','download link',函数(){
var id=this.id;
jQuery.ajax({
键入:“POST”,
url:ajaxurl,
数据:{“操作”:“计数下载”,
“id”:id
},
成功:功能(数据){
window.location=site.url+“/下载成功?fid=“+id;
}
});
});
在我添加带有链接的文件之前,一切正常。通常,这样的链接将开始下载.zip
文件,但即使在AJAX调用花费时间之后,页面也会重定向到下载成功页面,而不会触发下载
而且这种情况在大多数情况下都会发生,只有一到两次文件开始下载
p.S.:我测试过,但不是我的情况。试试这个
将带您进入另一页
success: function (data) {
window.location.href = site.url + "/download-success?fid="+ id;
}
将带您进入新窗口中的另一页
success: function (data) {
window.open('site.url + "/download-success?fid="+ id');
}
同意@marc。要添加更多信息,请执行此操作
<a href='download.php?file=some_file.zip'>Download</a>
试试这个,如果有任何问题,请告诉我。不要这样做。通过php脚本提供文件,并在那里进行下载计数更新。这将更加可靠:脚本将捕获文件的所有点击,而不仅仅是那些“礼貌地”通过html/js的点击。好的,记住你的建议,我可以成功地重定向页面,顺便说一句,文件下载是一个垃圾。所以,我使用了一堆代码,但在开始之前,我尝试了:
echo$file;如果(file_exists($file)){echo$file;}
,其中显示的是$file
,但文件_exists()
显示错误:警告:file_exists():找不到包装器“https”-配置PHP时忘记启用它了吗?
$\u GET['file']
必须防止不安全的输入!
//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);