强制浏览器使用Javascript window.open下载图像? [英] Force Browser to download Image with Javascript window.open?

查看:225
本文介绍了强制浏览器使用Javascript window.open下载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让图片一旦点击就可以下载(没有右键单击将图像另存为)?

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

我正在使用一个小的Javascript函数来调用下载页面:

I'm using a small Javascript function to call the download page:

<a href="#" 
   onclick="window.open('download.php?file=test.jpg', 'download', 'status=0');"
>Click to download</a>

在download.php页面中我有类似的内容:

In the download.php page I have something like:

$file = $_GET['file'];
header('Content-Description: File Transfer');
header("Content-type: image/jpg");
header("Content-disposition: attachment; filename= ".$file."");
readfile($file);

但它不起作用。我做错了什么?

提前致谢!

But it doesn't work. What am I doing wrong?
Thanks in advance!

推荐答案

使用 application / octet-stream 而不是 image / jpg


如果在具有application / octet-stream内容类型的响应中使用[Content-Disposition]标头,则隐含的建议是用户代理不应显示响应,而是直接输入保存响应为...对话框。

- RFC 2616 - 19.5.1内容处理

这篇关于强制浏览器使用Javascript window.open下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆