【问题标题】:How to dowload image from Telegram Web App如何从 Telegram Web App 下载图像
【发布时间】:2022-12-12 02:00:57
【问题描述】:

我有一个站点,其中仅包含一个图像。当有人点击它时,图像开始下载到用户的计算机。最近,我试图将它作为 Telegram Web App 启动,但碰巧出现了问题。当我在我的电脑上(作为 TG WebApp)点击图像时,一切正常,但是当我在我的手机(sumsung a51)(作为 TG WebApp)上点击它时,没有任何反应。 我网站的代码: HTML(仅正文):

<div class="wrapper">
    <img class="image" src="pathtoimg" alt="img"></img>
</div>
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<script src="./index.js"></script>

JS(索引.js):

async function toDataURL(url) {
    const blob = await fetch(url).then(res => res.blob());
    return URL.createObjectURL(blob);
}

async function downloadFile(src, filename) {
    const a = document.createElement("a");
    a.href = await toDataURL(src);
    a.download = filename;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
}

function main() {
    const image = document.querySelector(".image")
    if (image) {
        image.addEventListener("click", (e) => {
            downloadFile(image.src, "image.png")
        })
    }
}

main()

感谢所有试图帮助我的人,我很珍惜

【问题讨论】:

    标签: javascript html css telegram telegram-bot


    【解决方案1】:

    我不确定这是否适用于 Telegram Web Apps 现在。在移动浏览器中,您必须确认文件下载。这应该与 Web 应用程序一样工作,但目前尚未实现。好的功能创意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 2019-12-24
      • 2022-12-15
      相关资源
      最近更新 更多