node / express强制浏览器使用自定义名称下载文件

编程入门 行业动态 更新时间:2024-10-26 06:31:04
本文介绍了node / express强制浏览器使用自定义名称下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我已经为我的大学项目建立了一个节点/快递网站,在搜索法律ID后,它显示了一个大表,其中包含与该ID相关的不同格式和语言的所有文件。 我使用模块 http-proxy 请求并提供这些文件给客户。 当提供xml,xhtml,html和pdf文件(每个浏览器都能够直接查看它们)时,没有错误。 我有.zip和.rdf文件的问题。文件没有损坏,但是当我点击ZIP图标时,它会丢失原始名称

  • ,它给我下载提示, m丢失原始文件名(该文件将被称为proxy或proxy.zip,不同浏览器上的不同行为)
  • 当我点击RDF图标时,某些浏览器打开文件直接在浏览器中,一些浏览器将无法识别格式,一些浏览器想要以proxy的形式下载它)

所以我发现HTML5属性下载标签a。它只是解决我的问题,反正它不支持每个版本的Internet Explorer和Safari。浏览网页我发现一些解决方法,在IE或Safari浏览该页面之后添加右键单击并保存为...,但该解决方案不适用于我,因为我不是在说一个单一的链接但是一个充满链接的表。我的网站还需要在手机上工作。

有没有办法编写一些服务器端代码来强制浏览器使用自定义文件名下载文件? / p>

这是代码的一小段代码:

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配置文件,用于设置主机,端口和起始路径,文件包含。

提前感谢:)

解决方案

你需要在响应对象中添加一个新的头来指示文件名,并定期下载。

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

如果您想让浏览器尝试在其中打开文件,您也可以使用inline自己,像Chrome一样使用pdf文件。

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

根据@Thomas建议,也是一个很好的主意,包括总是正确的内容类型: p>

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

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

  • when i click on ZIP icon, it gives me the download prompt, but I'm losing the original file name (the file will be called "proxy" or "proxy.zip", different behaviors on different browsers)
  • when i click on RDF icon, some browsers opens the file directly in browser, some browsers won't recognize the format, some browsers wants to download it with name "proxy")

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 = ''+cfg.edb_opt.host+':'+cfg.edb_opt.port+cfg.edb_opt.rest+file; console.log('Proxy: serving '+realurl); proxy.web(req, res, { 'target': realurl }); });

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

Thanks in advance :)

解决方案

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");

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");

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强制浏览器使用自定义名称下载文件

本文发布于:2023-11-01 20:37:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   浏览器   名称   文件   node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!