问题描述
我有一个简单的的node.js + 的RESTify 后端与标准的CORS设置和这个端点:
var文件='1,5,8,11,12,13,176,567,9483 ';\r
\r
server.get('/下载'功能(REQ,资源,下一个){\r
res.set({内容处置:附件;文件名='numbers.csv'});\r
res.setHeader(内容类型,文本/ CSV);\r
res.send(文件);\r
返回下一个();\r
},功能(错误){\r
res.send(ERR);\r
});
\r
什么它该做的就是在创建CSV文件,并将其返回。
它的伟大工程时,我只是在端点地址,网页浏览器中键入并回车。该文件被正确下载。
但是,当我试图做同样的事情,但不是使用浏览器的地址栏我用Restangular这样的:
Restangular.one('下载')。获得()。然后(功能( RES){\r
的console.log(RES);\r
});
\r
只是写响应安慰,但没有文件被下载。
有没有办法做到这一点使用Restangular?或者,也许我需要用别的东西吗?
我不知道,如果Restangular能做到这一点,但我使用的脚本这样的东西。添加Filesaver到HTML的头,然后:
Restangular.one('下载')。获得()。然后(功能(RES){
var文件=新的Blob([RES] {类型:文本/ CSV'});
的saveAs(文件,'something.csv');
});
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?
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');
});
这篇关于从REST API与Restangular请求下载一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!