返回

koa 文件下载 pdf预览 两个接口 - nodejs - chromeDownload chromePreview

发布时间:2022-10-18 01:39:25 330
# chrome# npm

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​​ 用于梳理知识点



特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
react build本地相对目录 2022-10-18 01:24:58