困惑阿三 2023-11-25 13:13 采纳率: 0%
浏览 11

点击下载按钮 下载不了,如何解决?


  const onDownLoad = () => {
    const path = document.createElement("a");
// 路径 案例 https://t7.baidu.com/it/u=2745944624,2341188700&fm=193&f=GIF
    path.href = "https://t7.baidu.com/it/u=2745944624,2341188700&fm=193&f=GIF";
    path.download = "图片";

    path.style.display = 'none';  // 隐藏链接  

    document.body.appendChild(path);

    path.click();


    document.body.removeChild(path);  // 下载完成后移除a标签  
  }

  
点击事件 
   <Button
        onClick={onDownLoad}
      >下载</Button>

点击下载按钮 不会下载 而是 图片 不会下载 求告知

  • 写回答

2条回答 默认 最新

  • 余小又 2023-11-25 14:30
    关注
    async function getPic() {
      fetch('https://t7.baidu.com/it/u=2745944624,2341188700&fm=193&f=GIF')
        .then(response => response.blob())
        .then(blob => {
          const url = window.URL.createObjectURL(blob);
          const a = document.createElement('a');
          a.href = url;
          a.download = 'image.gif';
          document.body.appendChild(a);
          a.click();
          window.URL.revokeObjectURL(url);
        });
    
    
    }
    getPic()
    
    评论

报告相同问题?

问题事件

  • 创建了问题 11月25日