有没有一种方法可以在您点击图像后将其下载(无需右键单击将图像另存为)?
我正在使用一个小的 Javascript 函数来调用下载页面:
<a href="#"
onclick="window.open('download.php?file=test.jpg', 'download', 'status=0');"
>Click to download</a>
在 download.php 页面中我有类似的内容:
$file = $_GET['file'];
header('Content-Description: File Transfer');
header("Content-type: image/jpg");
header("Content-disposition: attachment; filename= ".$file."");
readfile($file);
但它不起作用。我做错了什么?
提前致谢!
最佳答案
使用application/octet-stream而不是 image/jpg:
If [the Content-Disposition] header is used in a response with the application/octet-stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as...' dialog.
— RFC 2616 – 19.5.1 Content-Disposition
关于php - 强制浏览器使用 Javascript window.open 下载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5648967/