惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Project Zero
Project Zero
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
Simon Willison's Weblog
Simon Willison's Weblog
S
Schneier on Security
B
Blog
NISL@THU
NISL@THU
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
AWS News Blog
AWS News Blog
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
P
Palo Alto Networks Blog
I
Intezer
美团技术团队
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
H
Heimdal Security Blog
T
Troy Hunt's Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Forbes - Security
Forbes - Security
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cisco Blogs
S
SegmentFault 最新的问题
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
AI
AI
WordPress大学
WordPress大学
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
小众软件
小众软件
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
G
GRAHAM CLULEY
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
H
Hacker News: Front Page

博客园 - givebest

Glarity Summary - 利用 ChatGPT为Bilibili及YouTube视频内容生成摘要 (免费无广告) Nodejs CMS——基于 NestJS/NuxtJS 的完整开源项目 Vee-validate 父组件获取子组件表单校验结果 Vue.js + Nuxt.js 项目中使用 Vee-validate 表单校验 又拍云 Node.js 实现文件上传、删除 Docz 用 MDX 写 React UI 组件文档 - givebest Mac 远程连接 Windows - givebest 更优雅的方式: JavaScript 中顺序执行异步函数 win10 系统右键菜单不显示文字(只有小图标)修复方法 JavaScript 排序算法(JavaScript sorting algorithms) JavaScript addEventListener 第三个参数 webpack 实现的多入口项目脚手架 Kindle电子阅读器收不到个人文档推送解决方案 纯CSS3大转盘抽奖(响应式、可配置) - givebest - 博客园 JavaScript简单分页,兼容IE6,~3KB 原生JavaScript实现hasClass、addClass、removeClass、toggleClass - givebest - 博客园 JavaScript闭包(Closure) GitHub Pages 绑定二级域名 - givebest HTML5 Canvas绘制转盘抽奖 - givebest - 博客园
Node.js 上传文件 - givebest - 博客园
givebest · 2017-12-31 · via 博客园 - givebest

Express + Multer

Node.js 上传文件 demo,基于 ExpressMulter

app.js

let express = require('express')
let multer = require('multer')

let app = express()
app.use(express.static(__dirname));

let storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'upload/')
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + '-' + file.originalname);
  }
});

let upload = multer({storage: storage});

app.post('/upload', function (req, res, next) {
  upload.single('file')(req, res, function (err) {
    if (err) {
      console.log('error', err);
      return;
    }

    res.send(JSON.stringify(req.file));
    console.log(req.file);
  });
});


app.set('port', process.env.PORT || 9009);

app.listen(app.get('port'), function () {
  console.log('http://localhost:' + app.get('port'));
});

index.html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="renderer" content="webkit">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta name="format-detection" content="telephone=no">
  <title>Node.js upload</title>
  <meta name="description" content="">
</head>

<body>
  <form id="upload" method="POST" action="/upload" enctype="multipart/form-data">
    <p>
      <label for="file">File:</label>
      <input type="file" name="file" required/>
    </p>
    <p>
      <input type="submit" name="submit" value="Submit" />
    </p>
  </form>

  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <script>
    $(function () {
      $('#upload').on('submit', function (e) {
        e.preventDefault();
        var formData = new FormData(this);
        $.ajax({
          type: 'POST',
          url: '/upload',
          data: formData,
          contentType: false,
          processData: false
        }).done(function () {
          alert('Done');
          console.log('done');
        }).fail(function (err) {
          console.log(err);
        })
      });
    });
  </script>
</body>
</html>

Repo

https://github.com/givebest/node-upload

Use

npm install
node app