【发布时间】:2017-12-30 09:18:17
【问题描述】:
我使用 npm papercut https://www.npmjs.com/package/papercut 进行图像上传,这是保存图像的功能(完美运行)。
uploader.process('image1', file.path, function(images){
console.log(images.avatar); // '/images/uploads/image1-avatar.jpg'
console.log(images.small); // '/images/uploads/image1-small.jpg'
})
我使用文件系统模块方法fs.stat,我想创建一个目录,我希望uploader.process 在fs.stat 回调中运行。所以保存的图像会进入fs.stat 创建的目录。这是我到目前为止的代码,我不知道将uploader.process 函数放在哪里,所以回调调用它。
fs.stat(`${tenantId}/`, function (err, stats){
if (err) {
// Directory doesn't exist or something.
console.log('Folder doesn\'t exist, so I made the folder ' + `${tenantId}/`);
return fs.mkdir(`assets/${tenantId}`, callback);
}
uploader.process('image1', file.path, function(images){
console.log(images.avatar); // '/images/uploads/image1-avatar.jpg'
console.log(images.small); // '/images/uploads/image1-small.jpg'
})
});
【问题讨论】:
-
file变量从何而来?为什么创建文件夹后返回,您不想在创建文件夹后处理图像?缺少某些代码,这是在路由器处理程序中吗? -
@MiguelLattuada
file来自我在此函数之外的变量var file = req.files,我在创建文件夹后返回uploader.image,因为这是我的问题,我不知道在哪里把它说成是回调可以调用该函数。我唯一缺少的代码是uploader.image的代码,我认为我不需要添加我的问题是在fs.stat回调
标签: javascript node.js callback fs