koa 文件下载 pdf预览 两个接口 - nodejs - chromeDownload chromePreview
koa 文件下载 pdf预览 两个接口 - nodejs - chromeDownload chromePreview
chrome.js
const router = require("koa-router")()
const mime = require('mime-types') //需npm安装
const fs = require('fs')
router.prefix("/chrome")
router.get("/", async (ctx, next) => {
ctx.body = {
title: "/api/chrome/",
}
})
// http://127.0.0.1:31188/api/chrome/chromePreview?E:\root\Personal\English\杨亮英语\【完结】有道词典高考词汇3500\pdf\02.pdf
// http://127.0.0.1:31188/api/chrome/chromePreview?C:\Users\Reciter\Desktop\桌面Fake垃圾桶\1.jpg
router.get('/chromePreview', async (ctx, next) => {
// console.info('ctx.request.query', ctx.request.query)
// let filePath = ctx.request.query.filePath
let filePath = Object.keys(ctx.request.query)[0]
let file = fs.readFileSync(filePath)
let mimeType = mime.lookup(filePath) //读取图片文件类型
ctx.set('content-type', mimeType) //设置返回类型
ctx.body = file //返回图片
})
// http://127.0.0.1:31188/api/chrome/chromeDownload?filePath=E:\root\Personal\English\杨亮英语\【完结】有道词典高考词汇3500\pdf\02.pdf
// http://127.0.0.1:31188/api/chrome/chromeDownload?filePath=C:\Users\Reciter\Desktop\桌面Fake垃圾桶\1.jpg
router.get('/chromeDownload', async (ctx, next) => {
let filePath = ctx.request.query.filePath
console.info('filePath', filePath)
let file = fs.readFileSync(filePath)
let fileName = filePath.split('\\').pop()
ctx.set('Content-disposition', 'attachment;filename=' + encodeURIComponent(fileName))
ctx.body = file //返回图片
})
module.exports = router
---------------------------------------------
生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!
https://pengchenggang.gitee.io/navigator/
SMART原则:
目标必须是具体的(Specific)
目标必须是可以衡量的(Measurable)
目标必须是可以达到的(Attainable)
目标必须和其他目标具有相关性(Relevant)
目标必须具有明确的截止期限(Time-based)
生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!
https://pengchenggang.gitee.io/navigator/
SMART原则:
目标必须是具体的(Specific)
目标必须是可以衡量的(Measurable)
目标必须是可以达到的(Attainable)
目标必须和其他目标具有相关性(Relevant)
目标必须具有明确的截止期限(Time-based)