在我正在做的 webapp 中,創建了 jsonarray 檔案。例如,它們看起來像:
[{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 CLI:12.0.5 節點:14.16.1 包管理器:npm 7.11.1 Angular:12.0.5
uj5u.com熱心網友回復:
Blob 不能將 json 物件作為引數(甚至 json 物件陣列)。您可以傳遞一個字串陣列,例如
new Blob(result.map(x=>JSON.stringify(x)), {type: "application/text"});
查閱 Blob檔案了解詳細資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/456747.html
