当前位置:网站首页>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
边栏推荐
- Is it safe to buy funds on Great Wall Securities?
- I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
- SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
- PHP reads ini or env type configuration
- ThreadLocal内存泄漏是什么,怎么解决
- 在证券账户上买基金安全吗?哪里可以买基金
- It's nothing to be utilitarian!
- Resumption of attack and defense drill
- 微信小程序缓存过期时间的相关设置(推荐)
- 实例讲解将Graph Explorer搬上JupyterLab
猜你喜欢

Review data desensitization system

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

.env.xxx 文件,加了常量,卻undefined

智能运维实战:银行业务流程及单笔交易追踪

USB-IF协会与各种接口的由来

- Oui. Env. Fichier XXX, avec constante, mais non spécifié
![Flow control statement of SQL data analysis [if, case... When detailed]](/img/7b/eabb0700936d34a3a145737580be88.png)
Flow control statement of SQL data analysis [if, case... When detailed]

Qt5.12.9 migration tutorial based on Quanzhi H3

Why does blocprovider feel similar to provider?

SQL数据分析之子查询的综合用法和案例题【耐心整理】
随机推荐
Iota in golang
leetcode96不同的二叉搜索樹
EMC circuit protection device for surge and impulse current protection
Kubernetes resource object introduction and common commands (III)
【CMake】Qt creator 里面的 cmake 配置
cookie、session、tooken
记录一下大文件上传偶然成功偶然失败问题
heketi 记录
It's nothing to be utilitarian!
Heketi record
Jielizhi, production line assembly link [chapter]
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
【mysql 07】GPG key retrieval failed: “Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022“
微信小程序缓存过期时间的相关设置(推荐)
Material design component - use bottomsheet to show extended content (I)
How excel opens CSV files with more than one million lines
二叉搜索树的创建,查找,添加,删除操作
Key points of security agreement
What is the purpose of ERP project implementation plan?
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