为什么不能 JQuery 设置锚 "download" 属性值?
Why can't JQuery set anchor "download" attribute value?
我正在尝试动态设置锚点下载属性的值。这应该是下载文件的文件名。但是,文件名默认为其原始值,而不是我要为其提供的值。
编辑: 有人指出下载属性值实际上在 DOM 中发生了变化。但是,下载文件的文件名分配不正确。
编辑: 似乎不是动态设置值的问题。即使是硬编码,文件名也没有设置。
<a href="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" download="image.png">click</a>
http://jsfiddle.net/abalter/2wz5zs7r/
HTML:
<button>Set url and download name for anchor</button>
<a href="" download="">Download Google logo</a>
Javascript:
var googleLogoURL = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png";
$('button').on('click', function(){
alert("url and filename set");
$('a').prop({
'href': googleLogoURL,
'download': "google-logo.png"
});
});
它不起作用,因为您所在的网页 运行 JS 可能没有托管在 https://www.google.com
。
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. (This is to protect users from being made to download sensitive personal or confidential information without their full understanding.)
来自MDN:
This attribute is only honored for links to resources with the same-origin.
我正在尝试动态设置锚点下载属性的值。这应该是下载文件的文件名。但是,文件名默认为其原始值,而不是我要为其提供的值。
编辑: 有人指出下载属性值实际上在 DOM 中发生了变化。但是,下载文件的文件名分配不正确。
编辑: 似乎不是动态设置值的问题。即使是硬编码,文件名也没有设置。
<a href="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" download="image.png">click</a>
http://jsfiddle.net/abalter/2wz5zs7r/
HTML:
<button>Set url and download name for anchor</button>
<a href="" download="">Download Google logo</a>
Javascript:
var googleLogoURL = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png";
$('button').on('click', function(){
alert("url and filename set");
$('a').prop({
'href': googleLogoURL,
'download': "google-logo.png"
});
});
它不起作用,因为您所在的网页 运行 JS 可能没有托管在 https://www.google.com
。
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. (This is to protect users from being made to download sensitive personal or confidential information without their full understanding.)
来自MDN:
This attribute is only honored for links to resources with the same-origin.