javascript - Download a file from REST API with Restangular request - Stack Overflow

I've got a simple node.js + Restify backend with standard CORS settings and this endpoint:var file

I've got a simple node.js + Restify backend with standard CORS settings and this endpoint:

var file = '1,5,8,11,12,13,176,567,9483';

server.get('/download', function(req, res, next) {
  res.set({"Content-Disposition": "attachment; filename='numbers.csv'"});
  res.setHeader("Content-type", "text/csv");
  res.send(file);
  return next();
}, function(err) {
  res.send(err);
});

I've got a simple node.js + Restify backend with standard CORS settings and this endpoint:

var file = '1,5,8,11,12,13,176,567,9483';

server.get('/download', function(req, res, next) {
  res.set({"Content-Disposition": "attachment; filename='numbers.csv'"});
  res.setHeader("Content-type", "text/csv");
  res.send(file);
  return next();
}, function(err) {
  res.send(err);
});

What it's suppose to do is to is to create CSV file and return it.

It works great when I simply type in the endpoint address to web browser and hit enter. The file gets downloaded properly.

But when I try to do the same thing, but instead of using browser's address bar I use Restangular like that:

Restangular.one('download').get().then(function (res) {
    console.log(res);
});

it just writes response to console, but no file is being downloaded.

Is there a way to do this using Restangular? Or maybe I need to use something else for this?

Share Improve this question asked Aug 4, 2015 at 22:32 zorzazorza 2,8943 gold badges30 silver badges43 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

I am not sure if Restangular can do that, but I am using FileSaver script for stuff like that. Add Filesaver to your HTML head and then:

Restangular.one('download').get().then(function (res) {
    var file = new Blob([res], { type: 'text/csv' });
    saveAs(file, 'something.csv');
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744112292a4559005.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信