使用 multer-ftp 重命名文件
Rename file using multer-ftp
我正在尝试使用 multer-ftp 将文件上传到 ftp。它成功上传到 ftp 但是我需要更改文件的名称。有办法吗?
var upload = multer({
storage: new FTPStorage({
basepath: '/path',
ftp: {
host: host,
secure: false,
user: user,
password: pwd
}
})
}).single('fileupload');
app.post('/getfiles', function (req, res, next) {
upload(req,res, function(err){
if(err){
res.send('Error uploading file - ' + err);
}else{
res.send('File is uploaded - ' + JSON.stringify(req.file));
}
})
})
在 req.file 中,它具有上传时的原始文件名。我怎样才能让 multer-ftp 使用该名称而不是它出现的名称上传文件(它出现的示例格式 5acfbabc8430fb3d311ae365f448.png
选中此 code,使用 destination
选项重命名文件。
var upload = multer({
storage: new FTPStorage({
basepath: '/path',
destination: function (req, file, options, callback) {
callback(null, path.join(options.basepath, file.originalname))
},
ftp: {
host: host,
secure: false,
user: user,
password: pwd
}
})
}).single('fileupload');
//更正后的代码
我正在尝试使用 multer-ftp 将文件上传到 ftp。它成功上传到 ftp 但是我需要更改文件的名称。有办法吗?
var upload = multer({
storage: new FTPStorage({
basepath: '/path',
ftp: {
host: host,
secure: false,
user: user,
password: pwd
}
})
}).single('fileupload');
app.post('/getfiles', function (req, res, next) {
upload(req,res, function(err){
if(err){
res.send('Error uploading file - ' + err);
}else{
res.send('File is uploaded - ' + JSON.stringify(req.file));
}
})
})
在 req.file 中,它具有上传时的原始文件名。我怎样才能让 multer-ftp 使用该名称而不是它出现的名称上传文件(它出现的示例格式 5acfbabc8430fb3d311ae365f448.png
选中此 code,使用 destination
选项重命名文件。
var upload = multer({
storage: new FTPStorage({
basepath: '/path',
destination: function (req, file, options, callback) {
callback(null, path.join(options.basepath, file.originalname))
},
ftp: {
host: host,
secure: false,
user: user,
password: pwd
}
})
}).single('fileupload');
//更正后的代码