当前位置:网站首页>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
边栏推荐
- js 公共库 cdn 推荐
- Jielizhi, production line assembly link [chapter]
- 创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
- cookie、session、tooken
- 【opencv】train&test HOG+SVM
- Graduation season is both a farewell and a new beginning
- Record the accidental success and failure of uploading large files
- 起床困难综合症(按位贪心)
- leetcode96不同的二叉搜索树
- MySQL: the difference between insert ignore, insert and replace
猜你喜欢
Relatively easy to understand PID understanding
Data analysis methodology and previous experience summary [notes dry goods]
[QT] QT cannot find a solution to the compiler using msvc2017
RPA tutorial 01: Excel automation from introduction to practice
Jielizhi, production line assembly link [chapter]
[QT] qtcreator uninstall and installation (abnormal state)
What is ThreadLocal memory leak and how to solve it
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
Ldr6035 smart Bluetooth audio can continuously charge and discharge mobile devices
实例讲解将Graph Explorer搬上JupyterLab
随机推荐
Leetcode96 different binary search trees
Is the securities account given by qiniu business school safe? Where can I open an account
[QT] QT cannot find a solution to the compiler using msvc2017
起床困难综合症(按位贪心)
Vue force cleaning browser cache
九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建
cookie、session、tooken
北京炒股开户选择手机办理安全吗?
UVM tutorial
Is it safe to choose mobile phone for stock trading account opening in Beijing?
【opencv】train&test HOG+SVM
Guide d'installation du serveur SQL
Windows 7 install MySQL error: 1067
Selectively inhibiting learning bias for active sampling
EMC circuit protection device for surge and impulse current protection
【QT】测试Qt是否能连接上数据库
Which app is better and more secure for stock mobile account opening
挖财学堂开户打新债安全可靠嘛?
Pytorch learning record
Halcon knowledge: an attempt of 3D reconstruction