javascript - 如何设置下载 header 以避免客户端 Windows Defender 阻止 javascript 文件

标签 javascript node.js

我刚刚尝试下载 JavaScript 文件,但 Windows Defender 阻止并删除了它。

在服务器中,http header 已设置,如下所示

res.setHeader('Content-Type', 'application/javascript');
res.setHeader('Content-Disposition', 'attachment; filename=' + filename);
res.write(data);
res.end();

我认为这在 jQuery 站点( http://jquery.com/download/ )中是可能的,但我也看到其他库选择重定向到纯文本页面。

我的标题有问题吗?有解决方法吗?

最佳答案

我假设您使用 Express,然后使用 Express sendFile ( http://expressjs.com/en/4x/api.html#res.sendFile ) 并只需设置 Content-Disposition header 。经过测试并可在最新的 Chrome 和 IE11 中运行。

var file = '/absolute/path/to/file/app.js';
var options = {
    headers: {
        'Content-Disposition': 'attachment; filename=foo.js'
    }
};
res.sendFile(file, options);

sendFile 将设置其余 header ,例如 Content-LengthContent-Type 本身。 顺便说一句,当涉及到 JQuery-s 下载链接时,他们使用 download 属性作为链接 - https://developer.mozilla.org/en/docs/Web/HTML/Element/a#attr-download 。但 download 属性的问题似乎在于并非所有主流浏览器都支持它 - http://caniuse.com/#search=download

关于javascript - 如何设置下载 header 以避免客户端 Windows Defender 阻止 javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34792823/

相关文章:

javascript - jQuery -> 更改 Window.href

javascript - 使用 Ajax 显示 PHP 成功和失败消息

node.js - Express.js Web 服务器在内存耗尽之前在生产环境中持续 5 分钟

javascript - NodeJS : Events on child processes

javascript - SequelizeJS HasOne 关联错误

javascript - 如何使用javascript在不刷新浏览器的情况下显示2个不同的按钮

javascript - array.filter 不从数组中删除值

javascript - 在父元素中使用 jQuery 获取数据属性的值

javascript - Google Shopping API node.js 产品插入返回 "INSERT request must specify product"错误

javascript - 访问调用模块中定义的所需模块中的函数