douyonglang4845 2019-04-16 15:14
浏览 40

php返回文件时,浏览器URL不会更改

So I have a WordPress plugin that creates a custom admin page, for example: http://localhost/wp/wp-admin/admin.php?page=download_file

This page has a button element that will call the following function on click:

function downloadFile(){
    window.location.href += '&download';
}

And I have my php configured in a way that if there is a download attribute present in the URL, it will return a file.

function download_file()
    {
        global $pagenow;
        if ($pagenow === 'admin.php' && $_REQUEST['page'] === 'download_file&download') {
            $fichero = 'file.txt';
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Content-Type: application/octet-stream");
            header("Content-Disposition: attachment; filename=$fileName");
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: " . filesize($fichero));
            @readfile($fichero);
        }
}

What I'm trying to understand is why the URL goes back to normal when this happens. (It stays as http://localhost/wp/wp-admin/admin.php?page=download_file, without the download attribute). If instead of returning a file, I echo anything else, then the url will keep the download attribute (http://localhost/wp/wp-admin/admin.php?page=download_file&download)

Does the browser react differently when a request to a url returns a file?

How can I take advantage of this behaviour to avoid changing the open page if later on I implement a condition to return a file and this condition isn't met, meaning that the url will return nothing? In this scenario, I would like the url to remain as http://localhost/wp/wp-admin/admin.php?page=download_file, without the attribute as it happens now.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 python变量和列表之间的相互影响
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)