如何编辑从 multer 返回的路径以删除特定单词?
How can I edit returned path from multer to remove a particualr word?
我正在使用 multer 和 express,当我上传文件时 multer 的存储引擎 returns 路径还包括 public 使用的文件夹通过表达静态,因为我正在使用 ejs 模板。有没有办法让返回的路径不包含 public 文件夹名称?
// 节点应用配置
const app = express();
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/profile', express.static('uploads'));
// 存储引擎
const storage = multer.diskStorage({
destination: './public/upload',
filename: (req, file, cb) => {
return cb(null, `${file.fieldname}_${Date.now()}${path.extname(file.originalname)}`)
}
})
const upload = multer({
storage: storage,
limits:{
fileSize: 10485760
}
})
function errHandler(err, req, res, next) {
if (err instanceof multer.MulterError) {
res.json({
success: 0,
message: err.message
})
}
}
app.use(errHandler);
// multer 返回的路径
public\upload\profile_1609198415143.PNG
有什么办法可以让返回的这个路径不包含"public\ "吗?
来自文档 File information, we know req.file.path
is the full path to the uploaded file. It generated by path.join(destination, filename)
statement, see disk.js#L37。
您可以像这样删除 public/
字符串:
console.log(req.file);
const path = req.file.path.split('/').slice(1).join('/');
console.log(path);
输出:
{ fieldname: 'file',
originalname:
'Stack Exchange personalized prediction data 2020-12-21.json',
encoding: '7bit',
mimetype: 'application/json',
destination: './public/upload',
filename: 'file_1609299871948.json',
path: 'public/upload/file_1609299871948.json',
size: 99765 }
upload/file_1609299871948.json
我正在使用 multer 和 express,当我上传文件时 multer 的存储引擎 returns 路径还包括 public 使用的文件夹通过表达静态,因为我正在使用 ejs 模板。有没有办法让返回的路径不包含 public 文件夹名称?
// 节点应用配置
const app = express();
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/profile', express.static('uploads'));
// 存储引擎
const storage = multer.diskStorage({
destination: './public/upload',
filename: (req, file, cb) => {
return cb(null, `${file.fieldname}_${Date.now()}${path.extname(file.originalname)}`)
}
})
const upload = multer({
storage: storage,
limits:{
fileSize: 10485760
}
})
function errHandler(err, req, res, next) {
if (err instanceof multer.MulterError) {
res.json({
success: 0,
message: err.message
})
}
}
app.use(errHandler);
// multer 返回的路径
public\upload\profile_1609198415143.PNG
有什么办法可以让返回的这个路径不包含"public\ "吗?
来自文档 File information, we know req.file.path
is the full path to the uploaded file. It generated by path.join(destination, filename)
statement, see disk.js#L37。
您可以像这样删除 public/
字符串:
console.log(req.file);
const path = req.file.path.split('/').slice(1).join('/');
console.log(path);
输出:
{ fieldname: 'file',
originalname:
'Stack Exchange personalized prediction data 2020-12-21.json',
encoding: '7bit',
mimetype: 'application/json',
destination: './public/upload',
filename: 'file_1609299871948.json',
path: 'public/upload/file_1609299871948.json',
size: 99765 }
upload/file_1609299871948.json