HTML code:
1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 4 <Head>5 <MetaCharSet= "UTF-8">6 <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0">7 <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">8 <title>Document</title>9 </Head>Ten One <Body> A <formAction= "Http://127.0.0.1/dopost"enctype= "Multipart/form-data"Method= "POST"> -Name:<inputtype= "text"name= "Name">Gender: - <inputtype= "Radio"name= "Gender"value= "Male">Man<inputtype= "Radio"name= "Gender"value= "female">female Age: the <inputtype= "Number"name= "Age">Photo: - <inputtype= "File"name= "img"> - <inputtype= "Submit"value= "Submit"> - </form> + </Body> - + </HTML>
JS Code
1Let HTTP = require (' http ');2Let formidable = require (' formidable ');3Let Util = require (' Util ');4 5 6 //Creating a server7Let Server = Http.createserver (function(req, res) {8 //If your access address is this, and the request type is post9 if(Req.url = = '/dopost ' && req.method.toLowerCase () = = ' Post ') {TenLet form =NewFormidable. Incomingform (); One //set File Upload storage address AForm.uploaddir = './uploads '; - //when the callback function is executed, the form is fully received. -Form.parse (req,function(Err, fields, files) { the if(ERR)Throwerr; -Console.log (' Upload file path: ', Files.img.path, ' upload filename: ', files.img.name); - //Console.log (Util.inspect ({fields:fields, files:files})); - //all text fields, radio boxes, are stored in field; + //all file domains, files -Res.writehead, {' Content-type ': ' Text/plain ' }); + ARes.end (' success!~~~ '); at }); - } - }); - -Server.listen (80, ' 127.0.0.1 ');
Node-post-Upload Image