当前位置:网站首页>Node——Egg 创建本地文件访问接口
Node——Egg 创建本地文件访问接口
2022-07-02 00:15:00 【一只漫步前行的羊】
1.Router添加路由
router.get('/file/:fileName', controller.fileController.file)
2. controller文件创建fileController.js
const Controller = require('egg').Controller;
class FileController extends Controller{
async file() {
const {
ctx } = this;
let res = await ctx.service.fileService.file()
ctx.body = res
}
}
module.exports = FileController
3. service文件创建fileService.js
const Service = require('egg').Service;
class FileService extends Service{
async file(){
const {
ctx } = this;
let fileName = ctx.params.fileName //获取文件名
let suffix = fileName.split(".")[fileName.split(".").length - 1] //获取文件后缀
ctx.set('Content-Type', `image/${
suffix}`)
let filePath = `E:\\development\\myProject\\pc-dev-tool\\file\\` //文件目录
return fs.readFileSync(`${
filePath}${
fileName}`) //读取文件并返回
}
}
module.exports = FileService
访问: http://localhost:8080/file/1238127391.png
边栏推荐
- cookie、session、tooken
- [embedded system course design] a single key controls the LED light
- [QT] qtcreator uninstall and installation (abnormal state)
- Openwrt enable kV roaming
- Using multithreaded callable to query Oracle Database
- 如何提升数据质量
- Data analysis methodology and previous experience summary [notes dry goods]
- Difficult to get up syndrome (bit by bit greed)
- 【QT】测试Qt是否能连接上数据库
- GCC compilation
猜你喜欢

使用VB.net将PNG图片转成icon类型图标文件

SQL Server 安裝指南

Digital transformation has a long way to go, so how to take the key first step

Guide d'installation du serveur SQL

Use vb Net to convert PNG pictures into icon type icon files

PyTorch学习记录
![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]
![Jielizhi, production line assembly link [chapter]](/img/f8/20c41ffe9468d59bf25ea49f73751e.png)
Jielizhi, production line assembly link [chapter]

【QT】Qt 使用MSVC2017找不到编译器的解决办法

数据分析方法论与前人经验总结【笔记干货】
随机推荐
Why does blocprovider feel similar to provider?
SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
数据分析方法论与前人经验总结【笔记干货】
Linux CentOS7安装Oracle11g的超完美新手教程
Openvino model performance evaluation tool DL workbench
.env.xxx 文件,加了常量,却undefined
Kubernetes resource object introduction and common commands (III)
使用htaccess文件禁止目录里的脚本执行权限
Jielizhi, production line assembly link [chapter]
Vue force cleaning browser cache
How excel opens CSV files with more than one million lines
ADO. Net SqlDataAdapter object
mysql之B tree 以及 B+tree
SQL数据分析之流程控制语句【if,case...when详解】
vue 强制清理浏览器缓存
Jielizhi, production line assembly link [chapter]
时间复杂度与空间复杂度
回顾数据脱敏系统
Download the online video m3u8 tutorial
[QT] solve the problem that QT MSVC 2017 cannot compile