从Firebase存储下载文件到iframe [英] From firebase storage download file to iframe

查看:56
本文介绍了从Firebase存储下载文件到iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,我无法将图像下载到iframe.而是将其下载到本地驱动器.

在JS中:

  storageRef.child('images/CopyPerson.jpg').getDownloadURL().then(function(url) {
  var iframe1 = document.getElementById('downloadedCourse');
  iframe1.src = url;
}).catch(function(error) {
  // Handle any errors
});

在html中:

  <iframe id="downloadedCourse" width="800" height="500" src=""></iframe>

但是,如果我使用img而不是iframe,它会按预期工作.我需要使用iframe的原因是因为我打算下载pdf文件.有人知道为什么吗?

我认为这是因为content-disposition标头设置为attachment而不是inline.尝试设置元数据来更改此设置,然后查看是否可行:

// Obviously you only have to set the metadata once per file
// Not every time you download the URL
storageRef.child('images/CopyPerson.jpg').updateMetadata({
  "contentDisposition": "inline"  
}).then(function() {
  return storageRef.child('images/CopyPerson.jpg').getDownloadURL()
}).then(function(url) {
  var iframe1 = document.getElementById('downloadedCourse');
  iframe1.src = url;
});

With below code, I can not download image to iframe. Instead, it downloaded to local drive.

in JS:

  storageRef.child('images/CopyPerson.jpg').getDownloadURL().then(function(url) {
  var iframe1 = document.getElementById('downloadedCourse');
  iframe1.src = url;
}).catch(function(error) {
  // Handle any errors
});

in html:

  <iframe id="downloadedCourse" width="800" height="500" src=""></iframe>

However, if I use img instead of iframe, it works as supposed. The reason I need use iframe is because I intend to download pdf file. Anyone knows why?

解决方案

I presume that this is because the content-disposition header is set to attachment instead of inline. Try setting the metadata to change this and see if that works:

// Obviously you only have to set the metadata once per file
// Not every time you download the URL
storageRef.child('images/CopyPerson.jpg').updateMetadata({
  "contentDisposition": "inline"  
}).then(function() {
  return storageRef.child('images/CopyPerson.jpg').getDownloadURL()
}).then(function(url) {
  var iframe1 = document.getElementById('downloadedCourse');
  iframe1.src = url;
});

这篇关于从Firebase存储下载文件到iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆