当前位置:网站首页>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为需要保存的路径

边栏推荐
- 【cesium】Load and locate 3D Tileset
- 【过一下7】全连接神经网络视频第一节的笔记
- 1068找到更多的硬币
- u-boot in u-boot, dm-pre-reloc
- flex布局青蛙游戏通关攻略
- Redis - 13. Development Specifications
- 【解码工具】Bitcoin的一些在线工具
- [cesium] 3D Tileset model is loaded and associated with the model tree
- Flutter学习4-基本UI组件
- Dashboard Display | DataEase Look at China: Data Presents China's Capital Market
猜你喜欢

Flutter学习5-集成-打包-发布

u-boot debugging and positioning means

将照片形式的纸质公章转化为电子公章(不需要下载ps)

Flex layout frog game clearance strategy

Structured light 3D reconstruction (1) Striped structured light 3D reconstruction

Flutter学习2-dart学习

jvm three heap and stack

The mall background management system based on Web design and implementation

Structured Light 3D Reconstruction (2) Line Structured Light 3D Reconstruction

Difference between for..in and for..of
随机推荐
The underlying mechanism of the class
How to quickly upgrade your Taobao account to a higher level
结构光三维重建(二)线结构光三维重建
类的底层机制
flex布局青蛙游戏通关攻略
淘宝账号如何快速提升到更高等级
RDD和DataFrame和Dataset
u-boot调试定位手段
[cesium] element highlighting
物理层的接口有哪几个方面的特性?各包含些什么内容?
Pycharm中使用pip安装第三方库安装失败:“Non-zero exit code (2)“的解决方法
OFDM 十六讲 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
The mall background management system based on Web design and implementation
MySQL基础(一)---基础认知及操作
多线程查询结果,添加List集合
【Untitled】
Flutter real machine running and simulator running
Distributed systems revisited: there will never be a perfect consistency scheme...
shell函数
redis 持久化