javascript - 在 JavaScript 中更改默认图像扩展名

标签 javascript php dom-events

我的网站中有一个页面,显示图像。当我右键单击图像并单击将图像另存为时,我得到默认名称。我想在右键单击时更改图像名称并保存。

例如:
在我的 HTML 代码中我有
图像 src="abc.jpeg"

当我右键单击时,我希望将此图像保存为 def.jpg

有办法做到这一点吗?

最佳答案

据我所知,你不能用 JavaScript 做到这一点。这只能在服务器端处理。

如果您使用 PHP,一个简单的解决方案是使用 header() .

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

当然,您可以用变量替换 download.pdf。

header('Content-Disposition: attachment; filename="' . $fileName .'"');

来自 PHP 文档:

If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.

关于javascript - 在 JavaScript 中更改默认图像扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9382478/

相关文章:

javascript - 如何从 Bootstrap 轮播中删除白色背景?

javascript - 设置对象原型(prototype)(示例中)

php - Laravel 权限被拒绝

javascript - Javascript 中的事件循环是否在单独的线程中执行?

javascript - 如何在 meteor/javascript 中禁用 html 元素上的鼠标单击事件

asp.net - 如何通过 FORM 从 ASP.Net MVC 堆栈调用存储过程并在 json 中返回它们?

javascript - 在 Node 中处理大量数据的最快方法

php - Safari 后退按钮不支持 PHP 注销 session

php - 通过连接从 2 个以上 mysql 表中获取数据。

javascript - 仅执行一次的事件处理程序