- ,它给我下载提示, 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强制浏览器使用自定义名称下载文件
发布评论