系统具有一种形式,在后端,我可以恢复该数据,但是我无法将这些数据下载到.txt文件中。
我使用的是fs.writefile ()
,但是将系统上传到云中时,它无法访问目标文件夹。
var new_ingreso = new Ingreso(req.body);
//I want to download the data from req.body in a file.txt
fs.writeFile(
'nameFile.txt',
new_ingreso.nameUser,
error => {
if (error)
console.log(error, 'el archivo no fue creado');
else
console.log('El archivo fue creado');
});
}
该文件的创建没有问题,我想要知道的是是否可以通过某种方式下载此文件,或者是否可以通过其他方式下载。
他在想办法,但我不确定如何继续。
var file = fs.writeFile(....);
您需要创建这样的路线。需要设置内容类型以使其下载文件而不是在浏览器中显示。没有安全性,没有经过测试的代码,但是它为您提供了实现方法的思路。
请参见https://expressjs.com/en/api.html#res.sendFile
get('file', (req, res) => {
res.sendFile('nameFile.txt', options, function (err) {
if (err) {
next(err)
} else {
console.log('Sent:', 'nameFile.txt')
}
})
})