文件下载的消息格式
-
服务器只要在响应头中加入
Content-Disposition: attachment; filename="xxx"
即可触发浏览器的下载功能,其中:attachment
表示附件,浏览器看到此字段,触发下载行为(不同的浏览器下载行为有所区别);filename="xxx"
这是告诉浏览器,保存文件时使用的默认文件名;
-
这部分操作是由服务器完成的,和前端开发无关;
启用迅雷下载
-
用户可能安装了某些下载工具,这些下载工具在安装时,都会自动安装相应的浏览器插件,只要对下载地址稍作修改,就会触发浏览器使用插件进行下载,当然,不同插件的地址规则不同;
-
比如,迅雷的下载地址规则为:
let url = 'thunder: //base64(AA下载地址ZZ)';
服务端
const express = require('express');
const path = require('path');
const app = express();
const port = 8000;
app.get('/download/:filename', (req, res) => {
const filename = path.join(__dirname, './res', req.params.filename);
res.download(filename, req.params.filename);
});
app.listen(port, () => {
console.log(`server listen on ${port}`);
});
客户端
<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>
</body>
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;
}
计算机网络🛜 文件上传
上一篇