在没有位置的情况下以PHP和AJAX下载zip文件。HREF

发布于 2025-01-23 11:35:00 字数 2184 浏览 2 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文