In this lesson we create a new Express web server app for handling file uploads and persisting them to the filesystem. We will walk through using the express-fileupload middleware module from npm to process file uploads, and then use the Express static middleware to serve the uploaded file as a static asset.

const path = require('path');
const express = require('express');
const fileUpload = require('express-fileupload');
const app = express(); app.use(fileUpload());
app.use('/uploads', express.static(path.join(__dirname, 'uploads'))); app.get('/', (req, res) => {
res.send(`
<form action="/upload" enctype="multipart/form-data" method="post">
<input type="file" name="foo" /><br /><br />
<input type="submit" value="Upload" />
</form>
`);
}); app.post('/upload', (req, res) => {
if (!req.files) return res.status(400).send('No files were uploaded!'); const { foo } = req.files;
const uploadTo = `uploads/${foo.name}`; foo.mv(uploadTo, (err) => {
if (err) return res.status(500).send(err); res.send(`File uploaded to <a href="${uploadTo}">${uploadTo}</a>`);
});
}); app.listen(8080, () => {
console.log('Server listening on port 8080!');
});

[Express] Upload Files with Express的更多相关文章

  1. [Node.js] Serve Static Files with Express

    In this lesson we will find out how to serve static assets (images, css, stylesheets, etc.) with Exp ...

  2. Upload Files To FTP in Oracle Forms D2k

    Upload Files To FTP in Oracle Forms D2k Use following procedure to upload files to Ftp.   PROCEDURE ...

  3. html5 & upload files

    html5 & upload files https://www.sitepoint.com/html5-ajax-file-upload/ https://www.webcodegeeks. ...

  4. How To Use XDOLoader to Manage, Download and Upload Files? (文档 ID 469585.1)

    Applies to: BI Publisher (formerly XML Publisher) - Version 5.6.3 to 5.6.3 [Release 5] Information  ...

  5. How To Use XDOLoader to Manage, Download and Upload Files? (DOC ID 469585.1)

    In this Document Goal Fix     Downloading Files   Uploading Files References Applies to: BI Publishe ...

  6. nodejs安装express以后,使用express显示不是内部或外部命令

    1.问题描述 在命令窗口通过npm install -g express 安装express以后,通过express -e express新建工程失败,提示express不是内部或外部命令 2.解决方 ...

  7. nodejs 实践:express 最佳实践(三) express 解析

    nodejs 实践:express 最佳实践(三) express 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固, ...

  8. nodejs 实践:express 最佳实践(六) express 自省获得所有的路由

    nodejs 实践:express 最佳实践(六) express 自省获得所有的路由 某些情况下,你需要知道你的应用有多少路由,这在 express 中没有方法可以.因此我这边曲线了一下,做成了一个 ...

  9. HTTPWebrequest上传文件--Upload files with HTTPWebrequest (multipart/form-data)

    使用HTTPWebrequest上传文件遇到问题,可以参考Upload files with HTTPWebrequest (multipart/form-data)来解决 https://stack ...

随机推荐

  1. [NOIP2012提高组]国王游戏

    题目:洛谷P1080.Vijos P1779.codevs1198. 题目大意:国王和每个大臣左.右手各写了一个数.规定每个大臣得到的金币数为他前面所有人左手的数字的乘积除以他自己右手的数(向下取整) ...

  2. 第一章、zabbix安装

    前言: 注意:本文不涉及性能测试.性能优化中的监控,所有文字的出发点都是日常运维监控. 在开始之前,我们还是先统一下认识:要监控一个对象,需要掌握哪些东西呢? 监控对象的理解:要监控的对象你是否了解呢 ...

  3. tab.py

    vim tab.py #!/usr/bin/env python # #Tab import sys import readline import rlcompleter import atexit ...

  4. JAVA JS 中的 modulus exponent 生成 C# 公匙

    C#用的是xml,里面是base64编码的.你上面的就是hex格式,只要把上面hex格式转成byte数组,然后在base64编码就可以了. public static byte[] Hex2Byte( ...

  5. HDFS 断点续传,写文件功能

    实际上这是个 HDFS 的工具类部分代码. 首先 public static Configuration configuration = null;public static FileSystem f ...

  6. HNU13303 Counting substhreengs(递推)

    题目:http://acm.hnu.cn/online/? action=problem&type=show&id=13303&courseid=0 题意:给你一个字符串,由数 ...

  7. KDD 2011 最佳工业论文中机器学习的实践方法-翻译

    作者:黄永刚 Practical machine learning tricks from the KDD 2011 best industry paper 原文链接:http://blog.davi ...

  8. Pycharm 的安装

    一. Windows 安装 汉化 破解补丁激活 下载 `https://pan.baidu.com/s/1qjI9uHaw0x374rwu6H8djA` 并将 JetbrainsCrack-2.8-r ...

  9. 分享一个vue中的vue-Resource用法

    //引入 <script src="//cdn.bootcss.com/vue-resource/1.2.1/vue-resource.js" type="text ...

  10. Codefroces B. T-primes

    http://codeforces.com/problemset/problem/230/B B. T-primes time limit per test 2 seconds memory limi ...