gpt4 book ai didi

JavaScript 检查是否有效的 base64 图像

转载 作者:行者123 更新时间:2023-12-03 06:51:26 25 4
gpt4 key购买 nike

有没有一种简单的方法来检查 base64 图像 url 是否有效?
我通过 ajax/xhr 从服务器获取 base64 url​​,并希望在我的网站上避免 xss。

最佳答案

我使用@wolfshirts 的答案编写了一个异步函数。它对我有用。

@wolfshirts function will not work because the return line was placedin the wrong scope.

async function isBase64UrlImage(base64String: string) {
let image = new Image()
image.src = base64String
return await (new Promise((resolve)=>{
image.onload = function () {
if (image.height === 0 || image.width === 0) {
resolve(false);
return;
}
resolve(true)
}
image.onerror = () =>{
resolve(false)
}
}))
}

关于JavaScript 检查是否有效的 base64 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32540364/

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