1. 程式人生 > >node express 上傳文件

node express 上傳文件

stringify .cn pos loaded read data body readfile ()

npm install --save multer

var fs = require(‘fs‘);
var multer  = require(‘multer‘);
var upload = multer({dest: ‘upload_tmp/‘});





router.post(‘/upload‘, upload.any(), function(req, res, next) {
     console.log(req.files[0]);  // 上傳的文件信息
 
     var des_file = "./" + 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 ) );
             }
         });
     });
 });

https://www.cnblogs.com/hammerc/p/8032838.html

node express 上傳文件