我已经编写了一些用于上传头像的中间件,如下所示:
var gm = require('gm'),
mkdirp = require('mkdirp'),
fs = require('fs');
uploadAvatar = function(req, res, next) {
var img, path, user;
if (req.files.avatar_image) {
user = req.user;
img = req.files.avatar_image;
path = __dirname + "/../../public/avatar/" + (user.name.parameterize()) + ".png";
mkdirp.sync(__dirname + "/../../public/avatar/");
fs.createReadStream(img.path).pipe(fs.createWriteStream(path));
gm(path).resize(250, 250).autoOrient().quality(90).write(path, function(err) {
if (err != null) {
req.flash('error', err);
} else {
user.avatar = "/avatar/" + (user.name.parameterize()) + ".png";
user.save(function(err) {
if (err != null) {
req.flash('error', err);
}
next();
});
}
});
} else {
next();
}
};
// Usage
app.post('/upload', ensureAuthenticated, uploadAvatar, handleUpload);
当我现在尝试上传图像时,node
由于非常有用的错误消息而崩溃:
events.js:72
throw er; // Unhandled 'error' event
^
Error: ENOENT, open '/tmp/1126846a248af5c584770b07de649f9b.png'
我也尝试过在使用之前复制文件gm
。我怀疑 express 在我触摸它之前就删除了文件,作为“安全性”。
有人能帮我一下吗?
编辑:完整来源