强制浏览器使用Javascript window.open下载图像?

编程入门 行业动态 更新时间:2024-10-26 00:19:51
本文介绍了强制浏览器使用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下载图像?

本文发布于:2023-11-01 20:36:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550349.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   浏览器   window   Javascript   open

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!