迅雷下载协议
- 拿到完整的下载地址
- 地址前面加上AA,后面加上ZZ
- 把第2步骤的组合的字符串base64编码
- 前面加thunder://base63编码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<a resrole="thunder" href="/res/hill.zip">下载</a>
<!-- 迅雷下载协议
把完整的下载地址得到
AA地址ZZ
base64编码
thunder://base64编码
-->
<script>
const a = document.querySelector("a[resrole=thunder]");
let thunderLink = `AA${a.href}ZZ`;
thunderLink = btoa(thunderLink);// base64编码
thunderLink = "thunder://" + thunderLink;
a.href = thunderLink;
</script>
</body>
</html>
创建一个图片下载的路由
const express = require('express');
const router = express.Router();
const path = require('path');
router.get('/:filename', (req, res, next) => {
const filename = req.params.filename;
const absPath = path.resolve(__dirname, '../../public/upload/', filename);
console.log('absPath=>', absPath);
res.download(absPath, filename, (err) => {
next(Error('filename is not exisit'));
})
})
module.exports = router;
初始化的引用路由
app.use('/api/upload', require('./api/upload'));
app.use('/api/download', require('./api/download'))
使用
在项目中,浏览器,或者post直接调用接口/api/download/filanme
既可就可以直接下载
- express会直接将请求头的
Content-type
设置为image/jpeg
,不用自己设置,是根据请求的文件名自动适配的 - Content-Dispostion: 告诉客户端,这个请求的处理方式,
attachment
以附件的方式处理,filename
,下载的时候默认的文件名 - *Accept-Ranges*:是否支持断点续传,服务器支持断点续传就会在头部中添加
Accept-Ranges
属性,断点续传的单位是字节bytes