这是我的路由器中管理我上传的部分:
fs.readFile(files.photo.path, function (err, data) {
// Here is the futur name of my file (ex: SERVER_PATH/images/moka/moka22/11/2016_1.jpg)
var newPath = __dirname + "/images/moka/moka" + new Date().toLocaleDateString() + "_" + Math.floor(Math.random() * 10) + 1 + "." + ext;
fs.writeFile(newPath, data, function (err) {
if(err) {
res.render('error', { error: err, message: "Erreur lors de l'upload"});
} else {
// insctructions
});
}
});
});当代码被触发时,我有以下错误:
Error: ENOENT: no such file or directory, open 'D:\projects\raspberry\routes\images\moka\moka2016-11-22_91.jpg'
at Error (native)如果我对fs文档(callback)有很好的理解,那么:
fs.writeFile(theNameOfTheFuturFile, theDataToPutIn, callback);所以我有点困惑。
对不起,我的英语可能不好,我希望你猜我的意思:)
谢谢。
发布于 2016-11-22 14:38:07
问题可能是您要写入的目录不存在。
因此,确保它的存在:
fs.readFile(files.photo.path, function (err, data) {
var dirPath = __dirname + "/images/moka/moka";
if (!fs.existsSync(dirPath)){
fs.mkdirSync(dirPath);
}
...或者用手做。
https://stackoverflow.com/questions/40744503
复制相似问题