我使用[Node][Angular] How to download excel files generated by node中提到的相同方式 但我仍然没有打开或保存任何弹出窗口。我可以在Chrome的网络窗口中看到缓冲区。
感谢任何帮助。
答案 0 :(得分:1)
在客户端,您可以使用:
$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
let blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const fileName = `file.xlsx`;
let file = new File([blob], fileName);
let fileUrl = URL.createObjectURL(file);
})
.error(function(data, status, headers, config){
});
我使用File因为它允许你传递文件名。您可以跳过此部分并将Blob传递给URL.createObjectURL
。它打开窗口下载。假设您的服务器代码是正确的。但是,如果您在Chrome中看到缓冲区而不是发送内容。
对于服务器端,我通常使用快递res.sendFile
。它需要您创建一个文件,以便您进行清理。