当前位置:网站首页>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
边栏推荐
- Heketi record
- Is the securities account given by qiniu business school safe? Where can I open an account
- [template] adaptive Simpson integral
- 智能运维实战:银行业务流程及单笔交易追踪
- [QT] solve the problem that QT MSVC 2017 cannot compile
- An intern's journey to cnosdb
- 求逆序数的三个方法
- Why does blocprovider feel similar to provider?
- MySQL: the difference between insert ignore, insert and replace
- 如何提升数据质量
猜你喜欢
[cmake] cmake configuration in QT Creator
如何提升数据质量
Selectively inhibiting learning bias for active sampling
Using multithreaded callable to query Oracle Database
Guide d'installation du serveur SQL
B tree and b+tree of MySQL
ThreadLocal内存泄漏是什么,怎么解决
RPA tutorial 01: Excel automation from introduction to practice
Data analysis methodology and previous experience summary [notes dry goods]
Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
随机推荐
4. Object mapping Mapstercover
Leetcode medium question sharing (5)
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
ADO. Net SqlCommand object
EMC circuit protection device for surge and impulse current protection
Is it safe to buy funds on Great Wall Securities?
时间复杂度与空间复杂度
Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]
vs2015 AdminDeployment. xml
Windows installation WSL (II)
Cmake engineering related
GCC compilation
PWN attack and defense world cgpwn2
Iota in golang
Database -- sqlserver details
Key points of security agreement
Kubernetes resource object introduction and common commands (III)
Algolia's search needs are almost closed
Which app is better and more secure for stock mobile account opening
LDR6035智能蓝牙音响可对手机设备持续充放电方案