Firebase downloadURL无效的URL [英] Firebase downloadURL invalid URL
问题描述
使用以下方法在firebase中生成downloadURL后
After generated downloadURL in firebase using:
{
...
let url = uploadTask.snapshot.downloadURL;
...
}
URL内容类似:
https://firebasestorage.googleapis.com/v0/b/sd2ds-.23.appspot.com/o/Usr%2FEls%2 ...
不用担心在浏览器或HTML标记中使用,但是,当我尝试使用在photoURL中生成的url时,会产生错误:
No worries in using in the browser or HTML tags, however, when I try to use the url generated in photoURL an error is generated:
photoURL字段必须是有效的URL.
我已经尝试使用 decodeURI(url),但没有任何改变.
I already tried use decodeURI(url) but nothing changed.
Firebase会在/(斜杠)处生成带有%2的网址,这似乎是问题所在.
Firebase generates the url with %2 in / (slash) place and seems be the problem.
有胶水吗?
谢谢
推荐答案
使用 decodeURIComponent .
具有错误处理功能的建议:
Suggestion of function with error handling:
const validUrl = require('valid-url');
const fillPhotoUrl = function(p) { return (p && validUrl.isUri(p)) ? decodeURIComponent(p) : null; };
然后使用它:
{
...
let url = fillPhotoUrl(uploadTask.snapshot.downloadURL);
...
}
如果您想知道解码URI和解码URIComponent之间的区别,请参阅此问题.
If you want to know the difference between decodeURI and decodeURIComponent, see this question.
这篇关于Firebase downloadURL无效的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!