我的网站中有一个页面,显示图像。当我右键单击图像并单击将图像另存为时,我得到默认名称。我想在右键单击时更改图像名称并保存。
例如:
在我的 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/