下载 jsonarray 文件 express, node, angular
Download jsonarray file express, node, angular
在我正在做的网络应用程序中,创建了 json 数组文件。例如,它们看起来像:
[{attr1:"123",attr2:"456"},{attr1:"abc",attr2:"def"}]
我正在尝试将这些 jsonarray 文件发送到客户端,它们应该完全按照在服务器中的方式下载,不应打开或解析。
我试过使用 send
、sendFile
和 download
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Disposition', 'attachment; filename=file.json');
res.sendFile(name, function (err) {
if (err) {
console.log('error sending:', name, err);
} else {
console.log('Sent:', name);
}
});
在前端我使用的是文件保护程序
this.Service.get(filename).subscribe(
result => {
console.log(result)
let blob = new Blob(result, {type: "application/json"});
saveAs(blob, collectionname+".json");
}
);
当我在客户端打开文件时,它看起来像这样:
[object Object][object Object]
但是我 console.log() 结果,我看到了 json 个对象。
我能做什么?
我的设置版本:
Angular命令行界面:12.0.5
节点:14.16.1
包管理器:npm 7.11.1
Angular: 12.0.5
Blob 不能将 json 个对象作为参数(甚至 json 个对象的数组也不行)。
您可以传递一个字符串数组,例如
new Blob(result.map(x=>JSON.stringify(x)), {type: "application/text"});
查看 Blob documentation 了解详情。
在我正在做的网络应用程序中,创建了 json 数组文件。例如,它们看起来像:
[{attr1:"123",attr2:"456"},{attr1:"abc",attr2:"def"}]
我正在尝试将这些 jsonarray 文件发送到客户端,它们应该完全按照在服务器中的方式下载,不应打开或解析。
我试过使用 send
、sendFile
和 download
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Disposition', 'attachment; filename=file.json');
res.sendFile(name, function (err) {
if (err) {
console.log('error sending:', name, err);
} else {
console.log('Sent:', name);
}
});
在前端我使用的是文件保护程序
this.Service.get(filename).subscribe(
result => {
console.log(result)
let blob = new Blob(result, {type: "application/json"});
saveAs(blob, collectionname+".json");
}
);
当我在客户端打开文件时,它看起来像这样:
[object Object][object Object]
但是我 console.log() 结果,我看到了 json 个对象。
我能做什么?
我的设置版本: Angular命令行界面:12.0.5 节点:14.16.1 包管理器:npm 7.11.1 Angular: 12.0.5
Blob 不能将 json 个对象作为参数(甚至 json 个对象的数组也不行)。 您可以传递一个字符串数组,例如
new Blob(result.map(x=>JSON.stringify(x)), {type: "application/text"});
查看 Blob documentation 了解详情。