简单实现nodejs上传功能

npm install formidable

var formidable = require('formidable'), http = require('http'), util = require('util'); http.createServer(function(req, res) { if (req.url == '/index' && req.method.toLowerCase() == 'post') { // parse a file upload var form = new formidable.IncomingForm(); form.uploadDir = './upload'; form.parse(req, function(err, fields, files) { console.log(fields); console.log(files); }); } }).listen(3001);

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="http://127.0.0.1:3001/index" method="post" enctype="multipart/form-data"> <p> 姓名:<input type="text"> </p> <p> 性别:<input type="radio" value="男">男 <input type="radio" value="女">女 </p> <p> 爱好:<input type="checkbox" value="吃饭">吃饭 <input type="checkbox" value="睡觉">睡觉 <input type="checkbox" value="游泳">游泳 </p> <p> 文件上传:<input type="file"> </p> <p> <input type="submit" value="提交"/> </p> </form> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwdjfj.html