zoukankan      html  css  js  c++  java
  • Node.js 上传文件

    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
    
  • 相关阅读:
    system函数
    如何:配置 ClickOnce 信任提示行为
    linux中shell变量$#,$@,$0,$1,$2的含义解释 (转载)
    C/C++中如何在main()函数之前执行一条语句?
    循环小数表示法
    struct/class等内存字节对齐问题详解
    malloc(0)
    C语言实现程序跳转到绝对地址0x100000处执行
    嵌入式程序设计中C/C++代码的优化
    backtrace和backtrace_symbols
  • 原文地址:https://www.cnblogs.com/givebest/p/8157363.html
Copyright © 2011-2022 走看看