node/express 强制浏览器下载自定义名称的文件 [英] node/express Force browser to download file with custom name

查看:38
本文介绍了node/express 强制浏览器下载自定义名称的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的大学项目建立了一个 node/express 网站,在搜索法律 ID 后,它显示了一个大表格,其中包含与此 ID 相关的不同格式和语言的所有文件.我使用模块http-proxy"来请求和提供这些文件给客户端.提供 xml、xhtml、html 和 pdf 文件时没有问题(每个浏览器都可以直接查看它们).我有 .zip 和 .rdf 文件的问题.文件未损坏但丢失了原始名称

I've built a node/express website for my university project that, after searching for an ID of a law, it shows a big table with all files in different formats and languages related with this id. I use the module "http-proxy" to request and serve these files to client. Nothing wrong when serving xml, xhtml, html and pdf files (every browser is able to directly view them). I have problems with .zip and .rdf files. Files are not corrupted but they are losing the original name

  • 当我点击 ZIP 图标时,它给了我下载提示,但我丢失了原始文件名(文件将被称为proxy"或proxy.zip",不同浏览器上的行为不同)
  • 莉>
  • 当我点击RDF图标时,有些浏览器直接在浏览器中打开文件,有些浏览器无法识别格式,有些浏览器想以代理"名称下载)

于是我发现了标签a"的 HTML5 属性下载".它只是解决了我的问题,无论如何并不是每个版本的 Internet Explorer 和 Safari 都支持它.在网上冲浪时,我找到了一些解决方法,当在 IE 或 Safari 中查看页面时,在 div 链接后添加右键单击并另存为...",但此解决方案不适合我,因为我不是在谈论单个链接但是一个充满链接的表格.而且我的网站也需要在手机上运行.

So I discovered the HTML5 attribute "download" of the tag "a". It just solve my problem, anyway it is not supported on every version of Internet Explorer and Safari. Surfing the web I found some workarounds to add "Right click and save as..." after a div link when the page is viewed in IE or Safari, but this solution is not for me, because i'm not talking about a single link but a table full of links. And my site need to work also on mobile phones.

有没有什么办法可以写一些服务器端的代码来强制浏览器下载自定义文件名的文件?

Is there any way to write some server-side code to force browsers to download files with a custom file name?

这是代理的一小段代码:

Here is the small piece of code of the proxy:

var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({ ignorePath: true });

app.get('/proxy', function(req , res) {
    var file = req.query.file;
    var realurl = 'http://'+cfg.edb_opt.host+':'+cfg.edb_opt.port+cfg.edb_opt.rest+file;
    console.log('Proxy: serving '+realurl);
    proxy.web(req, res, { 'target': realurl });
});

所有 cfg* 变量都来自一个 json 配置文件,用于设置包含文件的主机、端口和起始路径.

All cfg* variables comes from a json configuration file to set the host, port, and starting path where files are contained.

提前致谢:)

推荐答案

你需要给响应对象添加一个新的标头来指示文件名并进行常规下载.

You need to add a new header to the response object to indicate the file name and do a regular download.

res.set("Content-Disposition", "attachment;filename=somefile.ext");

如果您希望浏览器尝试自行打开文件,您也可以使用内联",就像 Chrome 处理 pdf 文件一样.

You could also use "inline" if instead you want the browser to try to open the file within it self, like with Chrome does with pdf files.

res.set("Content-Disposition", "inline;filename=somefile.ext");

根据@Thomas 的建议,始终包含正确的内容类型也是一个好主意:

As per @Thomas suggestion, also is a good idea to include always the right content type:

res.set("Content-Type", "application/octet-stream");

这篇关于node/express 强制浏览器下载自定义名称的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆