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://www.VuejsDev.com 用于梳理知识点