当前位置:网站首页>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
边栏推荐
- leetcode96不同的二叉搜索樹
- Windows installation WSL (II)
- PyCharm调用matplotlib绘图时图像弹出问题怎么解决
- 比较通俗易懂的PID理解
- Multi table operation - one to one, one to many and many to many
- SQL Server Installation Guide
- 【mysql 07】GPG key retrieval failed: “Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022“
- Soft exam information system project manager_ Compiled abbreviations of the top ten management processes to help memory recitation - -- software test advanced information system project manager 054
- [es practice] safe operation mode on ES
- B tree and b+tree of MySQL
猜你喜欢
![Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]](/img/3a/cced28a2eea9f9a0d107baabd01119.png)
Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]

Review data desensitization system
![[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler](/img/35/e458fd437a0bed4bace2d6d65c9ec8.png)
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler

Multi table operation - one to one, one to many and many to many

使用多线程Callable查询oracle数据库

Correlation - intra group correlation coefficient

Why does blocprovider feel similar to provider?

【QT】QtCreator卸载与安装(非正常状态)

LeetCode中等题题分享(5)

Heketi record
随机推荐
Guide d'installation du serveur SQL
Vue force cleaning browser cache
[embedded system course design] a single key controls the LED light
回顾数据脱敏系统
const // It is a const object... class nullptr_ t
[cmake] cmake configuration in QT Creator
S32Kxxx bootloader之UDS bootloader
Relevant settings of wechat applet cache expiration time (recommended)
Windows 7 install MySQL error: 1067
Which securities company is the best to open a stock account? Is there a security guarantee
攻防演练复盘
在证券账户上买基金安全吗?哪里可以买基金
cookie、session、tooken
智能运维实战:银行业务流程及单笔交易追踪
Review data desensitization system
Relatively easy to understand PID understanding
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
ADO. Net SqlConnection object usage summary
[QT] test whether QT can connect to the database