当前位置:网站首页>Node -- egg implements the interface of uploading files
Node -- egg implements the interface of uploading files
2022-07-02 00:20:00 【A walking sheep】
1. To configure config > config.default.js
config.multipart = {
mode: 'file'
}
2. controller File creation 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 File creation fileService.js
const Service = require('egg').Service;
class FileService extends Service{
async upload(){
const {
ctx } = this;
const files = ctx.request.files; // Get the information uploaded by the front end files Object group
const filePath = files[0].filepath.replace(/\\\\/g, "\/") // Get the temporary storage address of the file
const fileinfo = fs.readFileSync(filePath); // Read the file
const target = "E:\\development\\myProject\\pc-dev-tool\\upload\\fileName.png"; // Storage address
fs.writeFileSync(target, fileinfo) // write file
fs.unlinkSync(filePath) // Delete temporary storage files
return "ok"
}
}
module.exports = FileService
边栏推荐
- 【QT】對於Qt MSVC 2017無法編譯的問題解决
- LDR6035智能蓝牙音响可对手机设备持续充放电方案
- 求逆序数的三个方法
- Linux centos7 installation Oracle11g super perfect novice tutorial
- 北京炒股开户选择手机办理安全吗?
- Resumption of attack and defense drill
- 【CMake】Qt creator 里面的 cmake 配置
- Iota in golang
- .env.xxx 文件,加了常量,卻undefined
- Which app is better and more secure for stock mobile account opening
猜你喜欢
SQL数据分析之流程控制语句【if,case...when详解】
Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
Difficult to get up syndrome (bit by bit greed)
Selectively inhibiting learning bias for active sampling
Windows installation WSL (II)
Qt5.12.9 migration tutorial based on Quanzhi H3
牛客-练习赛101-推理小丑
Heketi record
Jielizhi, production line assembly link [chapter]
Download the online video m3u8 tutorial
随机推荐
. env. XXX file, with constant, but undefined
[QT] solve the problem that QT MSVC 2017 cannot compile
【QT】QtCreator卸载与安装(非正常状态)
S32Kxxx bootloader之UDS bootloader
ERP项目施行计划的目的是什么?
Example explanation: move graph explorer to jupyterlab
攻防演练复盘
SQL数据分析之子查询的综合用法和案例题【耐心整理】
【CMake】Qt creator 里面的 cmake 配置
Leetcode 96 différents arbres de recherche binaires
Use the htaccess file to prohibit the script execution permission in the directory
heketi 记录
Is it safe to buy funds on Great Wall Securities?
ADO. Net SqlDataAdapter object
起床困难综合症(按位贪心)
Linux centos7 installation Oracle11g super perfect novice tutorial
Multi table operation - one to one, one to many and many to many
Data analysis methodology and previous experience summary [notes dry goods]
4. Object mapping Mapstercover
An intern's journey to cnosdb