Simple implementation of nodejs upload function

  • 2021-07-12 04:30:18
  • OfStack

In this paper, we share the specific code of Android9 palace picture display for your reference. The specific contents are as follows

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>
  Name :<input type="text" name="name">
 </p>

 <p>
  Gender :<input type="radio" name="sex" value=" Male "> Male 
 <input type="radio" name="sex" value=" Female "> Female 
 </p>

 <p>
  Hobbies :<input type="checkbox" name="hobby" value=" Eat "> Eat 
 <input type="checkbox" name="hobby" value=" Sleep "> Sleep 
 <input type="checkbox" name="hobby" value=" Swimming "> Swimming 
 </p>
 <p>
  File upload :<input type="file" name="images">

 </p>
 <p>
 <input type="submit" value=" Submit "/>
 </p>

</form>


</body>
</html>

Related articles: