我想将上传响应发送回客户端后从app/
目录中删除文件。如何使用nodejs实现该任务?
server.js
var fs = require('fs');
var multer = require('multer');
var upload = multer({dest:'app/'}).single('file');
export function create(req, res) {
upload(req, res, function (err) {
if (err) {
console.log("error occurred");
}else{
console.log(req.file.path);
var fileContent = fs.readFileSync(req.file.path,'utf8');
res.json(fileContent);
}
});
}
答案 0 :(得分:2)
这样做:
var fileContent = fs.readFileSync(req.file.path,'utf8');
fs.unlink(req.file.path);
res.json(fileContent);
我建议不要使用同步功能:
fs.readFile(req.file.path,'utf8', function(err, data) {
if (err) throw err;
res.json(data);
fs.unlink(req.file.path);
});