gpt4 book ai didi

Javascript 下载属性在保存图像 dataUrl 时不起作用

转载 作者:行者123 更新时间:2023-11-30 12:41:24 24 4
gpt4 key购买 nike

我正在使用 chrome。

这是我的代码。

var save = document.createElement('a');
save.href = fileURL;
save.download = fileName;
alert(save.download);
save.click();

效果很好,但不会更改图像名称。它保存为“download.png”。

怎么了?

最佳答案

查看 fileURLfileName 设置的内容会有所帮助,但我猜这是您的问题。在 Firefox 和 Chrome 中,您必须使用图像的相对路径。它不适用于远程图像。

MDN说:

In Firefox 20 this attribute is only honored for links to resources with the same-origin.

我已经在 Chrome 和 Firefox 中测试过了,只有当你使用图像的相对路径时它才有效:

 save.href = "images/wonky-download-121938718712348891912.jpg";
save.download = "coolName.jpg";

我说相对路径是因为使用 mysite.com/image.jpg 无效,而 image.jpg 有效。

更新

whatwg.org说:

In cross-origin situations, the download attribute has to be combined with the Content-Disposition HTTP header, specifically with the attachment disposition type, to avoid the user being warned of possibly nefarious activity.

关于Javascript 下载属性在保存图像 dataUrl 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24318867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com