gpt4 book ai didi

javascript - 在新表中从 Firebase 存储打开文件而不下载

转载 作者:行者123 更新时间:2023-12-05 06:33:11 25 4
gpt4 key购买 nike

我在 Firebase 存储中有多个存储桶,它们都包含一个 html 文件。我想做的是通过 Javascript,获取存储桶中特定 HTML 文件的 URL,然后在新选项卡中打开它。当我使用“getDownloadURL”方法并将 url 传递给 window.open 时,我发现一个新选项卡暂时打开,文件被下载,然后选项卡关闭。理想情况下,我想完全避免下载文件,而只是查看它。我相信这是因为 URL 本身的格式而发生的: https://firebasestorage.googleapis.com/v0/b/[project].appspot.com/o/[bucket]%2Fcreate.html?alt=media&token=[token]

有人可以帮我使用 window.load 只在新标签页中打开 html 文件吗?我宁愿根本不下载文件,我需要选项卡保持打开状态。在此先感谢您的帮助!

var storage = firebase.storage();
var storageRef = storage.ref();
var reportFileRef = storageRef.child(currentUid + '/create.html');
reportFileRef.getDownloadURL().then(function(url) {
window.open(url, '_blank')
});

最佳答案

正确设置元数据解决了 Content-Disposition 问题。这是我定义的函数,其中 contenttype 是“text/html”或“image/png”

def uploadToFirebaseStorage(filename, filepath, user, report, contenttype):
my_file = open(os.path.join(filepath, filename), "rb")
my_bytes = my_file.read()
my_url = "https://firebasestorage.googleapis.com/v0/b/[project].appspot.com/o/" + filename
my_headers = { "Content-Type": contenttype }
my_request = urllib.request.Request(my_url, data=my_bytes, headers=my_headers)
try:
loader = urllib.request.urlopen(my_request)
except urllib.error.URLError as e:
message = json.loads(e.read())
return message["error"]["message"]
else:
return loader.read()

关于javascript - 在新表中从 Firebase 存储打开文件而不下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50878942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com