admin管理员组

文章数量:1664350

文件下载

对于前端 文件下载简单来说就是 a元素里面的href属性替换成目标下载文件的url

<a href="http://localhost:8000/download/Wallpaper1.jpg">

文件下载的消息格式

服务器只要在响应头中加入Content-Disposition: attachment; filename="xxx"即可触发浏览器的下载功能

其中:

  • attachment 表示附件,浏览器看到此字段,触发下载行为(不同的浏览器下载行为有所区别)

  • filename=“xxx”,这是告诉浏览器,保存文件时使用的默认文件名

这部分操作是由服务器完成的,和前端开发无关

启用迅雷下载

用户可能安装了某些下载工具,这些下载工具在安装时,都会自动安装相应的浏览器插件,只要对下载地址稍作修改,就会触发浏览器使用插件进行下载,当然,不同插件的地址规则不同

比如,迅雷的下载地址规则为:


thunder://base64(AA地址ZZ)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <a data-res="thunder" href="http://localhost:8000/download/Wallpaper1.jpg">
      下载桌面壁纸1
    </a>
    <a data-res="thunder" href="http://localhost:8000/download/Wallpaper2.jpg">
      下载桌面壁纸2
    </a>

    <script>
      const links = document.querySelectorAll('[data-res=thunder]');
      for (const link of links) {
        const base64 = btoa(`AA${link.href}ZZ`);
        const href = `thunder://${base64}`;
        link.href = href;
      }
    </script>
  </body>
</html>

请注意,这个示例代码中使用了 JavaScript 内置的 btoa() 函数来进行 Base64 编码。如果你需要在 Node.js 环境下运行这个函数,你需要使用 Buffer.from() 函数来进行 Base64 编码。

本文标签: 迅雷文件