gpt4 book ai didi

javascript - 如何将 URL 转换为 base64 而不输出 '/' 字符?

转载 作者:行者123 更新时间:2023-11-28 05:44:11 27 4
gpt4 key购买 nike

问题:我可以确定 Base64 编码的 URL 不会输出“/”字符吗?

背景:Firebase 使用键/值结构及其键名称 per the docs ,

"can include any unicode characters except for . $ # [ ] / and ASCII control characters 0-31 and 127"

我想使用 URL 作为我的一个集合的键,但显然是“/”和“.”禁止使用原始字符串。

我的计划(我没有结婚)是使用浏览器的函数(atob()btoa())将 URL 转换为 Base64或专用功能/NPM 模块 ( as discussed here )。

但是,Base64 输出可以包含“/”,这会违反 Firebase 规则。

URL 可能包含的字符会产生“/”吗?

如果是这样,有什么理由我不应该在 Base64 编码函数的前面/后面添加一个简单的 String.replace()

最佳答案

采纳 OP 评论中的建议(感谢 ceejayoz、Derek),看起来像这样会起作用:

let rawUrl = "http://stackoverflow.com/questions/38679286/how-can-i-convert-urls-to-base64-without-outputting-characters?noredirect=1#comment64738129_38679286";

let key = btoa(encodeURIComponent(url));
let decodedUrl = decodeURIComponent(atob(key));

rawUrl == decodedUrl // True

关于javascript - 如何将 URL 转换为 base64 而不输出 '/' 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38679286/

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