当前位置:网站首页>NodeJs接收上传文件并自定义保存路径
NodeJs接收上传文件并自定义保存路径
2022-08-05 05:13:00 【Ly666_ever】
//接收上传文件并自定义保存路径
router.post('/upload', (req, res) => {
//创建formidable表单解析对象
const form = new formidable.IncomingForm();
//保留上传文件的后缀名字
form.keepExtensions = true;
//设置上传文件的保存路径
form.uploadDir = path.join(__dirname, 'uploads');
//解析客户端传递过来的formData对象
form.parse(req, (err, fields, files) => {
oldPath = files.file.newFilename;
console.log(fields);
oldPath = path.join(__dirname, './uploads/' + oldPath);
var name = path.join(__dirname, './' + fields.url + '/' + files.file.originalFilename);
if (!name.includes(__dirname))//规定在当前路径内,防止滥传路径出现问题
res.send('路径异常');
dirExists(path.join(__dirname, './' + fields.url)).then(() => {
fs.rename(oldPath, name, function (err) {
if (err) {
console.error("改名失败" + err);
}
})
res.send(fields);
})
})
})测试
url为需要保存的路径

边栏推荐
- 结构光三维重建(二)线结构光三维重建
- "Recursion" recursion concept and typical examples
- 基于Web的商城后台管理系统的设计与实现
- Analysis of Mvi Architecture
- 社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
- Reverse theory knowledge 4
- 第四讲 back propagation 反向传播
- redis 缓存清除策略
- uboot开启调试打印信息
- The mall background management system based on Web design and implementation
猜你喜欢
随机推荐
OFDM 十六讲 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
延迟加载js方式async与defer区别
第二讲 Linear Model 线性模型
Error creating bean with name 'configDataContextRefresher' defined in class path resource
Opencv中,imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) 报错:error:!_src.empty() in function ‘cv::cvtColor‘
The role of DataContext in WPF
What field type of MySQL database table has the largest storage length?
Flutter Learning 4 - Basic UI Components
LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]
Requests the library deployment and common function
WPF中DataContext作用
redis 持久化
Requests库部署与常用函数讲解
Reverse theory knowledge 4
MySQL Foundation (1) - Basic Cognition and Operation
How to quickly upgrade your Taobao account to a higher level
2023年信息与通信工程国际会议(JCICE 2023)
1068找到更多的硬币
jvm three heap and stack
【记一下1】2022年6月29日 哥和弟 双重痛苦









