在没有位置的情况下以PHP和AJAX下载zip文件。HREF
我正在使用JQuery Ajax在PHP中创建一个ZIP文件下载系统。 当用户单击下载按钮时,请求将转到服务器并返回文件名。 我可以下载该文件,但它显示了整个文件的名称以及控制台中的文件路径。我不想要这个。
<button onclick="checkPayment(this.id)" id="filetoDownload_1">Download File</button>
JavaScript
function checkPayment(data){
$.ajax({
type: 'post',
url: '../download.php',
data: data,
async: false,
success: function(result) {
let returnkd = JSON.parse(result);
if(returnkd["status"]){
$("#success").html(`<div class="card"><div class="card-body bg-white text-black">Download will start soon...</div></div>`);
setTimeout(() => {
window.location.href = returnkd["filename"];
}, 2000);
}else{
$("#error").html(`<div class="card"><div class="card-body bg-white">Something wents wrong... contact admin</div></div>`);
}
}
});
}
PHP
// wothout header
if(isset($_POST)){
some code
return array("status"=>true,"filename"=>"../folder/filename.zip");
}
// with header
if(isset($_POST)){
some code here
downloadFile($filename);
return array("status"=>true,"filename"=>"../folder/filename.zip");
}
function downloadFile($filename)
{
$filename = $filename;
$filepath = "../".$filename;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($filepath));
ob_end_flush();
@readfile($filepath);
}
如何下载文件而不重定向window.location.href
或在控制台中显示filepath。 我尝试了PHP标头,但它不起作用
I am creating a zip file download system in php with jquery ajax.
When the user clicks the download button, the request goes to the server and returns the filename.
I am able to download the file but it shows the name of the entire file along with the file path in console. I do not want this.
<button onclick="checkPayment(this.id)" id="filetoDownload_1">Download File</button>
Javascript
function checkPayment(data){
$.ajax({
type: 'post',
url: '../download.php',
data: data,
async: false,
success: function(result) {
let returnkd = JSON.parse(result);
if(returnkd["status"]){
$("#success").html(`<div class="card"><div class="card-body bg-white text-black">Download will start soon...</div></div>`);
setTimeout(() => {
window.location.href = returnkd["filename"];
}, 2000);
}else{
$("#error").html(`<div class="card"><div class="card-body bg-white">Something wents wrong... contact admin</div></div>`);
}
}
});
}
PHP
// wothout header
if(isset($_POST)){
some code
return array("status"=>true,"filename"=>"../folder/filename.zip");
}
// with header
if(isset($_POST)){
some code here
downloadFile($filename);
return array("status"=>true,"filename"=>"../folder/filename.zip");
}
function downloadFile($filename)
{
$filename = $filename;
$filepath = "../".$filename;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($filepath));
ob_end_flush();
@readfile($filepath);
}
how to download file without redirecting window.location.href
or showing filepath in console.
i have tried php header but it doesn't work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论