从url angular 2下载文件

时间:2017-08-15 10:59:58

标签: html node.js

我有网址:http://localhost:9999/file/bongda.PNG

我使用nodejs服务于公共文件:

application.use(express.static(path.join(__dirname, 'uploads')));

application.get('/file/:name', function (req, res, next) {

  var options = {
    root: __dirname + '/uploads/',
    dotfiles: 'deny',


    headers: {
        'x-timestamp': Date.now(),
        'x-sent': true
    }
  };
    console.log('Express server listening downloads ');
  var fileName = req.params.name;
  res.type('png'); 
  res.sendFile(fileName, options, function (err) {
    if (err) {
      next(err);
    } else {
      console.log('Sent:', fileName);
    }
  });

});

我想从网址下载文件

我使用<a href="window.location.href='http://localhost:9999/file/bongda.PNG'">123123</a>

<a href="http://localhost:9999/file/bongda.PNG">123123</a>

但它没有成功。 请帮帮我?

2 个答案:

答案 0 :(得分:0)

您错过了href的javascript:

<a href="javascript:window.location.href='http://localhost:9999/file/bongda.PNG'">123123</a>

如果您想强制浏览器下载文件而不是打开它,则应在后端代码中添加以下行:

res.setHeader('Content-disposition', 'attachment; filename=' + fileName);

答案 1 :(得分:0)

public Response getClienteFornitore(
    @ApiParam(value = "Authorization token", required = true) @HeaderParam("Authorization")  String authorization,
    @HeaderParam("StaySignedIn")  boolean staySignedIn, 
    @PathParam("clienteFornitoreId") int clienteFornitoreId) {
    ...
}

我使用html5 href。确定

相关问题