【发布时间】:2018-11-26 02:01:49
【问题描述】:
我想强制下载存储在 Firebase 存储中的图像,但 HTML 锚点中的 download 属性不支持跨域,我无法将内容类型更改为 application/octet-stream,因为它已被使用生成缩略图。
怎么做?
【问题讨论】:
标签: firebase google-cloud-storage google-cloud-functions firebase-storage
我想强制下载存储在 Firebase 存储中的图像,但 HTML 锚点中的 download 属性不支持跨域,我无法将内容类型更改为 application/octet-stream,因为它已被使用生成缩略图。
怎么做?
【问题讨论】:
标签: firebase google-cloud-storage google-cloud-functions firebase-storage
在这种情况下,您不能在 html 锚点中使用简单的“下载”。
您可以做的是通过 javascript 发送下载请求。
有官方样本可供下载。
storageRef.child('images/stars.jpg').getDownloadURL().then(function(url) {
// `url` is the download URL for 'images/stars.jpg'
// This can be downloaded directly:
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function(event) {
var blob = xhr.response;
};
xhr.open('GET', url);
xhr.send();
// Or inserted into an <img> element:
var img = document.getElementById('myimg');
img.src = url;
}).catch(function(error) {
// Handle any errors
});
【讨论】:
img 可显示或form 可下载。这应该对creating a download form 有所帮助或将其设置为img、var img = document.getElementById('myimg'); img.src = url;