Gahing's blog Gahing's blog
首页
知识体系
  • 前端基础
  • 应用框架
  • 工程能力
  • 应用基础
  • 专业领域
  • 业务场景
  • 前端晋升 (opens new window)
  • Git
  • 网络基础
  • 算法
  • 数据结构
  • 编程范式
  • 编解码
  • Linux
  • AIGC
  • 其他领域

    • 客户端
    • 服务端
    • 产品设计
软素质
  • 面试经验
  • 人生总结
  • 个人简历
  • 知识卡片
  • 灵感记录
  • 实用技巧
  • 知识科普
  • 友情链接
  • 美食推荐 (opens new window)
  • 收藏夹

    • 优质前端信息源 (opens new window)
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Gahing / francecil

To be best
首页
知识体系
  • 前端基础
  • 应用框架
  • 工程能力
  • 应用基础
  • 专业领域
  • 业务场景
  • 前端晋升 (opens new window)
  • Git
  • 网络基础
  • 算法
  • 数据结构
  • 编程范式
  • 编解码
  • Linux
  • AIGC
  • 其他领域

    • 客户端
    • 服务端
    • 产品设计
软素质
  • 面试经验
  • 人生总结
  • 个人简历
  • 知识卡片
  • 灵感记录
  • 实用技巧
  • 知识科普
  • 友情链接
  • 美食推荐 (opens new window)
  • 收藏夹

    • 优质前端信息源 (opens new window)
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 前端基础

  • 应用框架

  • 工程能力

  • 应用基础

  • 专业领域

    • 服务端

      • Deno

      • Node.js

        • Node.js 版本切换
        • Node子进程执行ping操作并获取统计信息
        • Node 中的当前目录路径
        • node小bug记录
        • 事件驱动理解
        • 小技巧:Chrome 在线调试 Node
        • 深入浅出Node.js
        • log4js配置详解
        • node 模块源码解析

        • nodejs文件下载
      • 服务端框架

    • 跨端技术

    • Web IDE

    • 中后台

    • 动效渲染

    • 可视化

    • 埋点监控

    • 多媒体

    • 桌面技术

    • 游戏互动

    • 编辑器

    • 虚拟化与容器化

    • 设计系统

  • 业务场景

  • 大前端
  • 专业领域
  • 服务端
  • Node.js
gahing
2018/04/03

nodejs文件下载

本文来谈谈 nodejs 文件下载的细节

注:这边的文件下载,发起者为客户端

function (req, res, next) {
  //项目根目录下的/files/xls/demo.xlsx文件
  var filePath = path.join(__dirname, '../') + '/files/xls/demo.xlsx'
  // 定位到具体文件
  var stats = fs.statSync(filePath)
  //判断文件是否存在
  if (stats.isFile()) {
    //Content-Disposition属性,如果不考虑中文的话,直接用filename=demo.xlsx即可
    //如果有中文,需要进行utf8编码,filename后需带上*=UTF-8''
    // 如有中文但不进行编码设置,Content-Disposition参数将不生效,还是原来的文件名
    res.set({
      'Content-Type': 'application/octet-stream',
      'Content-Disposition': "attachment; filename*=UTF-8''" + encodeURI('App提交模板.xlsx'),
      'Content-Length': stats.size
    })
    res.send(stats)
    //fs.createReadStream(filePath).pipe(res)
  } else {
      res.status(404).end()
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

对于小文件来说,采用res.send(buffer) 或res.sendFile(fileName)的形式,是先将文件内容放到buff中再进行传输

若文件相对较大,需要采用fs.createReadStream(filePath).pipe(res)用流管道的方式传输,这种方式读一部分写一部分,用过的部分会被GC,故占内存少

express 还对res封装了一个download方法,已采用res.set进行参数设置,并调用res.sendFile()方法进行文件传输,用起来很方便

var filePath = path.join(__dirname, '../') + '/files/xls/demo.xlsx'
res.download(filePath, 'App提交模板.xlsx')
1
2

当文件不存在时(err),是走路由的Error Handler.若指定第三个参数(err)=>{}则走该参数

//文件不存在时产生的err
err:
  code:"ENOENT"
  errno:-4058
  expose:false
  message:"ENOENT: no such file or directory, stat 'e:\WebProjects\client-management-server\files\xls\demos.xlsx'"
  path:"e:\WebProjects\client-management-server\files\xls\demos.xlsx"
  stack:"Error: ENOENT: no such file or directory, stat 'e:\WebProjects\client-management-server\files\xls\demos.xlsx'"
  status:404
  statusCode:404
  syscall:"stat"
1
2
3
4
5
6
7
8
9
10
11

故小文件(<500M),推荐直接采用express的download方法

编辑 (opens new window)
上次更新: 2024/09/01, 23:56:56
yallist
Express开发总结

← yallist Express开发总结→

最近更新
01
浅谈代码质量与量化指标
08-27
02
快速理解 JS 装饰器
08-26
03
Vue 项目中的 data-v-xxx 是怎么生成的
09-19
更多文章>
Theme by Vdoing | Copyright © 2016-2024 Gahing | 闽ICP备19024221号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式