博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
express发送文件_使用Express发送文件
阅读量:2511 次
发布时间:2019-05-11

本文共 1189 字,大约阅读时间需要 3 分钟。

express发送文件

Express provides a handy method to transfer a file as attachment: Response.download().

Express提供了一种方便的方法来将文件作为附件传输: Response.download()

Once a user hits a route that sends a file using this method, browsers will prompt the user for download.

一旦用户点击使用此方法发送文件的路由,浏览器就会提示用户进行下载。

The Response.download() method allows you to send a file attached to the request, and the browser instead of showing it in the page, it will save it to disk.

Response.download()方法允许您发送附加到请求的文件,并且浏览器会将其保存到磁盘,而不是在页面中显示。

app.get('/', (req, res) => res.download('./file.pdf'))

In the context of an app:

在应用程序的上下文中:

const express = require('express')const app = express()app.get('/', (req, res) => res.download('./file.pdf'))app.listen(3000, () => console.log('Server ready'))

You can set the file to be sent with a custom filename:

您可以将文件设置为使用自定义文件名发送:

res.download('./file.pdf', 'user-facing-filename.pdf')

This method provides a callback function which you can use to execute code once the file has been sent:

此方法提供了一个回调函数,一旦文件发送完毕,便可以使用该函数执行代码:

res.download('./file.pdf', 'user-facing-filename.pdf', (err) => {  if (err) {    //handle error    return  } else {    //do something  }})

翻译自:

express发送文件

转载地址:http://whqgb.baihongyu.com/

你可能感兴趣的文章
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
人工智能暑期课程实践项目——智能家居控制(一)
查看>>
前端数据可视化插件(二)图谱
查看>>
kafka web端管理工具 kafka-manager【转发】
查看>>
获取控制台窗口句柄GetConsoleWindow
查看>>
Linux下Qt+CUDA调试并运行
查看>>
51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
查看>>
OKMX6Q在ltib生成的rootfs基础上制作带QT库的根文件系统
查看>>
zabbix
查看>>
多线程基础
查看>>
完美解决 error C2220: warning treated as error - no ‘object’ file generated
查看>>
使用SQL*PLUS,构建完美excel或html输出
查看>>
SQL Server数据库笔记
查看>>
X-Forwarded-For伪造及防御
查看>>
android系统平台显示驱动开发简要:LCD驱动调试篇『四』
查看>>
Android 高仿微信头像截取 打造不一样的自定义控件
查看>>