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

__EOF__

本文作者Reciter
本文链接https://www.cnblogs.com/pengchenggang/p/16093232.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   彭成刚  阅读(241)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-04-02 油猴 tamperMonkey 在百度首页 添加自己的自定义链接
2009-04-02 我的网站,我自己做的小架构平台。有爱好的来评价评价。带图
2009-04-02 FCKeditor 编辑器中把字号改为 14px 的方法,默认12px太难看
点击右上角即可分享
微信分享提示