我有一个文件夹,其中包含我使用npm package excel4node构建的所有excel表。
现在,我想将这些文件导出到依赖文件名称的用户。
我该怎么做?
我尝试引用此帖子,尽管我不明白如何将这些文件导出到用户。
如果我正确地提出了您的问题,则在客户端拨打API调用时可能会发送Excel文件。为此,这里有一些伪代码:
app.get('/user/:username', function(req, res) {
const username = req.params['username'];
const filePath = "/path/to/"+username+".excel"
// Check if file specified by the filePath exists
fs.exists(filePath, function(exists){
if (exists) {
//send the file to client
res.sendFile(filePath);
}
});
})
Note* res.sendfile((由Express V4.8.0启用。
现在根据您的评论,如果您想向客户端发送所有文件列表,则必须返回一系列字符串并在UI上渲染。
app.get('/user/all', function(req, res) {
const testFolder = "/path/to/folder";
// read the folder and set the result in a variable
var listOfFiles = fs.readdirSync(testFolder);
// send a JSON or any other response with these strings
res.json({"allFiles" : listOfFiles});
})