<a download='file' href="https://tinyjpg.com/images/social/website.jpg">
Download
</a>
<a download='file' href="https://tinyjpg.com/images/social/website.jpg">
Download
</a>
你可能会感到困扰,因为Firefox和Chrome 65+只支持同源下载链接,这可能是作为一种安全措施。
来源:https://caniuse.com/#feat=download(请参阅“已知问题”选项卡)
Web超文本应用技术工作组(WHATWG)建议,在跨源场景中(如您的示例),托管图像/文件的Web服务器需要发送Content-Disposition
HTTP头以使download=
得到尊重。
来源:https://html.spec.whatwg.org/multipage/links.html#downloading-resources
简而言之:
如果满足以下条件,你只能使用<a download='...'></a>
来强制下载图片/文件:
Content-Disposition
HTTP头字段表示应该下载。Content-Disposition
的设置可以允许下载呢?或者这个设置可以在我们的API中定义吗? - gnifrus也许你已经解决了,但是由于你将文件托管在 S3
上(请参见 Peter B 的回答中的评论),你需要使用 AWS SDK 给文件 URL 添加签名,并将 ResponseContentType
设置为 binary/octet-stream
。我正在使用 Node,所以代码如下:
const promises = array.map((item) => {
const promise = s3.getSignedUrlPromise('getObject', {
Bucket: process.env.S3_BUCKET,
Key: key, //filename
Expires: 604800, //time to expire in seconds (optional)
ResponseContentType: 'binary/octet-stream'
});
return promise;
});
const links = await Promise.all(promises);
async function downloadImage(imageSrc) {
const image = await fetch(imageSrc)
const imageBlog = await image.blob()
const imageURL = URL.createObjectURL(imageBlog)
const link = document.createElement('a')
link.href = imageURL
link.download = 'image file name here'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
setTimeout(function() {
url = 'https://media.sproutsocial.com/uploads/2017/02/10x-featured-social-media-image-size.png';
// downloadFile(url); // UNCOMMENT THIS LINE TO MAKE IT WORK
}, 2000);
// Source: http://pixelscommander.com/en/javascript/javascript-file-download-ignore-content-type/
window.downloadFile = function (sUrl) {
//iOS devices do not support downloading. We have to inform user about this.
if (/(iP)/g.test(navigator.userAgent)) {
//alert('Your device does not support files downloading. Please try again in desktop browser.');
window.open(sUrl, '_blank');
return false;
}
//If in Chrome or Safari - download via virtual link click
if (window.downloadFile.isChrome || window.downloadFile.isSafari) {
//Creating new link node.
var link = document.createElement('a');
link.href = sUrl;
link.setAttribute('target','_blank');
if (link.download !== undefined) {
//Set HTML5 download attribute. This will prevent file from opening if supported.
var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
link.download = fileName;
}
//Dispatching click event.
if (document.createEvent) {
var e = document.createEvent('MouseEvents');
e.initEvent('click', true, true);
link.dispatchEvent(e);
return true;
}
}
// Force file download (whether supported by server).
if (sUrl.indexOf('?') === -1) {
sUrl += '?download';
}
window.open(sUrl, '_blank');
return true;
}
window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
你的链接应该有一个ID来强制下载:
<a download='website.jpg' id='blablabla' href="https://tinyjpg.com/images/social/website.jpg">
Download
</a>