Force Browser to download Image with Javascript window.open?

前端 未结 11 882
感动是毒
感动是毒 2020-11-27 19:37

Is there a way to make an image a download once you click on it (without right-click save image as)?

I\'m using a small Javascript function to call the do

11条回答
  •  我在风中等你
    2020-11-27 20:03

    I think you forgot to add Path on the header

    if(isset($_GET['file'])){
        //Please give the Path like this
        $file = 'images/'.$_GET['file'];
    
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }
    }
    

提交回复
热议问题