当前位置:网站首页>Node——Egg 实现上传文件接口
Node——Egg 实现上传文件接口
2022-07-02 00:15:00 【一只漫步前行的羊】
1. 配置 config > config.default.js
config.multipart = {
mode: 'file'
}
2. controller文件创建fileController.js
const Controller = require('egg').Controller;
class FileController extends Controller{
async upload() {
const {
ctx } = this;
let res = await ctx.service.fileService.upload()
ctx.body = res
}
}
module.exports = FileController
3. service文件创建fileService.js
const Service = require('egg').Service;
class FileService extends Service{
async upload(){
const {
ctx } = this;
const files = ctx.request.files; //获取前端上传的files对象组
const filePath = files[0].filepath.replace(/\\\\/g, "\/") //获取文件临时存储地址
const fileinfo = fs.readFileSync(filePath); //读取文件
const target = "E:\\development\\myProject\\pc-dev-tool\\upload\\fileName.png"; //存储地址
fs.writeFileSync(target, fileinfo) //写入文件
fs.unlinkSync(filePath) //删除临时存储文件
return "ok"
}
}
module.exports = FileService
边栏推荐
- vue 强制清理浏览器缓存
- MySQL Replication中并行复制怎么实现
- 13 MySQL constraint
- S32Kxxx bootloader之UDS bootloader
- Windows installation WSL (II)
- [es practice] safe operation mode on ES
- Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]
- Qt5.12.9 migration tutorial based on Quanzhi H3
- SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
- Ldr6035 smart Bluetooth audio can continuously charge and discharge mobile devices
猜你喜欢

ThreadLocal内存泄漏是什么,怎么解决

. env. XXX file, with constant, but undefined

Halcon knowledge: an attempt of 3D reconstruction
![Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]](/img/24/d9a48a0f76cde65421edda04d0f854.png)
Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]

Selectively inhibiting learning bias for active sampling

Use vb Net to convert PNG pictures into icon type icon files

Key points of security agreement
![Various global files related to [.Net core] program](/img/89/32623abf30d3dc92a3cdb1710a624f.png)
Various global files related to [.Net core] program

LeetCode中等题题分享(5)

Mysql database driver (JDBC Driver) jar package download
随机推荐
. env. XXX file, with constant, but undefined
SQL Server 安装指南
求逆序数的三个方法
Download the online video m3u8 tutorial
How to realize parallel replication in MySQL replication
The origin of usb-if Association and various interfaces
Database -- sqlserver details
Flow control statement of SQL data analysis [if, case... When detailed]
Ldr6035 smart Bluetooth audio can continuously charge and discharge mobile devices
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
RPA教程01:EXCEL自动化从入门到实操
Linux centos7 installation Oracle11g super perfect novice tutorial
vs2015 AdminDeployment. xml
MySQL: the difference between insert ignore, insert and replace
Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]
SQL Server 安裝指南
It's nothing to be utilitarian!
EMC circuit protection device for surge and impulse current protection
Selectively inhibiting learning bias for active sampling
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电