Home > Article > Web Front-end > Code example for multiparty to implement file upload in nodejs
The content of this article is about the code example of multiparty implementation of file upload in nodejs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
var multiparty = require('multiparty'); function fileUpload(req,res) { //生成multiparty对象,并配置上传目标路径 var form = new multiparty.Form({uploadDir: '../static/images/'}); //上传完成后处理 form.parse(req, function(err, fields, files) { var filesTmp = JSON.stringify(files,null,2); if(err){ } else { var inputFile = files.file[0]; var uploadedPath = inputFile.path; var dstPath = '../static/images/' + inputFile.originalFilename; //重命名为真实文件名 fs.rename(uploadedPath, dstPath, function(err) { if(err){ console.log('rename error: ' + err); } else { console.log('rename ok'); } }); } res.send({"fileName":inputFile.originalFilename}); }); } route.post('/fileUpload',(req,res)=>{ fileUpload(req,res); });
Code source: https://github.com/chengyaxin/vue-test/blob/master/server/api.js
This is the code I wrote about the vue example , if you are interested, you can take a look at this example!
vue example renderings:
Related recommendations:
How to use js The setInterval timer method implements the carousel chart (complete code)
Encapsulates get requests and post requests in the mini program into global functions (code)
The above is the detailed content of Code example for multiparty to implement file upload in nodejs. For more information, please follow other related articles on the PHP Chinese website!