迅雷下载协议

  1. 拿到完整的下载地址
  2. 地址前面加上AA,后面加上ZZ
  3. 把第2步骤的组合的字符串base64编码
  4. 前面加thunder://base63编码
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <a resrole="thunder" href="/res/hill.zip">下载</a>
  10. <!-- 迅雷下载协议
  11. 把完整的下载地址得到
  12. AA地址ZZ
  13. base64编码
  14. thunder://base64编码
  15. -->
  16. <script>
  17. const a = document.querySelector("a[resrole=thunder]");
  18. let thunderLink = `AA${a.href}ZZ`;
  19. thunderLink = btoa(thunderLink);// base64编码
  20. thunderLink = "thunder://" + thunderLink;
  21. a.href = thunderLink;
  22. </script>
  23. </body>
  24. </html>

创建一个图片下载的路由

  1. const express = require('express');
  2. const router = express.Router();
  3. const path = require('path');
  4. router.get('/:filename', (req, res, next) => {
  5. const filename = req.params.filename;
  6. const absPath = path.resolve(__dirname, '../../public/upload/', filename);
  7. console.log('absPath=>', absPath);
  8. res.download(absPath, filename, (err) => {
  9. next(Error('filename is not exisit'));
  10. })
  11. })
  12. module.exports = router;

初始化的引用路由

  1. app.use('/api/upload', require('./api/upload'));
  2. app.use('/api/download', require('./api/download'))

使用

在项目中,浏览器,或者post直接调用接口/api/download/filanme既可就可以直接下载
image.png

  • express会直接将请求头的Content-type设置为image/jpeg,不用自己设置,是根据请求的文件名自动适配的
  • Content-Dispostion: 告诉客户端,这个请求的处理方式,attachment 以附件的方式处理,filename,下载的时候默认的文件名
  • *Accept-Ranges*:是否支持断点续传,服务器支持断点续传就会在头部中添加Accept-Ranges属性,断点续传的单位是字节bytes