express上传文件

npm install --save multer

index.js

var upload = require('./routes/upload');
app.use('/upload', upload);

update.js

var fs = require('fs');
var express = require('express');
var multer  = require('multer');

var router = express.Router();
var upload = multer({dest: 'upload_tmp/'});

router.post('/', upload.any(), function(req, res, next) {
    console.log(req.files[0]);  // 上传的文件信息

    var des_file = "./upload/" + req.files[0].originalname;
    fs.readFile( req.files[0].path, function (err, data) {
        fs.writeFile(des_file, data, function (err) {
            if( err ){
                console.log( err );
            }else{
                response = {
                    message:'File uploaded successfully',
                    filename:req.files[0].originalname
                };
                console.log( response );
                res.end( JSON.stringify( response ) );
            }
        });
    });
});

module.exports = router;

前端上传代码




    
    文件上传


    

文件上传:

选择一个文件上传:

你可能感兴趣的:(express上传文件)