当前位置:网站首页>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为需要保存的路径
边栏推荐
- [Decoding tools] Some online tools for Bitcoin
- Error creating bean with name 'configDataContextRefresher' defined in class path resource
- Flutter真机运行及模拟器运行
- 『递归』递归概念与典型实例
- 【读书】长期更新
- 02.01-----参数的引用的作用“ & ”
- DOM及其应用
- 【过一下3】卷积&图像噪音&边缘&纹理
- How can Flutter parent and child components receive click events
- The underlying mechanism of the class
猜你喜欢
随机推荐
redis 持久化
【cesium】加载并定位 3D Tileset
【过一下3】卷积&图像噪音&边缘&纹理
【Untitled】
Dephi reverse tool Dede exports function name MAP and imports it into IDA
有用番茄来监督自己的同道中人吗?加一下我的自习室,一起加油
[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
OFDM 十六讲 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
Mesos学习
【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)
Flutter真机运行及模拟器运行
2022 The 4th C.Easy Counting Problem (EGF+NTT)
【过一下9】卷积
C#关于set()和get()方法的理解及使用
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
Flutter学习4-基本UI组件
Difference between for..in and for..of
Requests the library deployment and common function
【微信小程序】WXML模板语法-条件渲染
类的底层机制