Firesbase存储-强制下载图像

时间:2018-11-26 02:01:50

标签: firebase google-cloud-storage google-cloud-functions firebase-storage

我想强制下载存储在Firebase Storage中的图像,但是HTML锚点中的“下载”属性不支持跨域,并且我无法将内容类型更改为“应用程序/八位字节流” ”,因为它用于生成缩略图。

怎么办?

1 个答案:

答案 0 :(得分:3)

在这种情况下,您不能在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
});