//I am learning Nodejs with the idea of PHP
varExpress = require (' Express ');varRouter =Express. Router ();varFS = require (' FS ');varPath= require ("path");varFormidable = require (' formidable '));/*GET home page.*/Router.get (‘/‘,function(req, res, next) {Res.render (' Index ', {title: ' Express+ejs+mysql+s2 ' });}); Router.post ('/file-upload ',function(req, res, next) {Console.log (' Start file Upload ... '); varform =NewFormidable. Incomingform (); //Set Editform.encoding = ' Utf-8 '; //Set file storage pathForm.uploaddir = "./public/images/"; //reserved suffixForm.keepextensions =true; //set the single file size limitForm.maxfieldssize = 2 * 1024 * 1024; //form.maxfields = 1000; Set so the sum of the size of the fileForm.parse (req,function(Err, fields, files) {//console.log (fields);Console.log (Files.thumbnail.path); Console.log (' File name: ' +files.thumbnail.name); varT = (NewDate ()). GetTime (); //Generate random numbers varran = parseint (Math.random () * 8999 +10000); //get the extension varExtname =Path.extname (files.thumbnail.name); //path.normalize ('./path//upload/data/. /file/./123.jpg '); Canonical format file name varOldPath =path.normalize (Files.thumbnail.path); //the new PathLet newfilename=t+ran+Extname; varNewPath = './public/images/' +NewFileName; Console.warn (' OldPath: ' +oldpath+ ' NewPath: ' +NewPath); Fs.rename (Oldpath,newpath,function(err) {if(Err) {Console.error ("Renaming failed" +err); } res.render (' Index ', {title: ' File Upload succeeded: ', imginfo:newfilename}); }); //Res.end (Util.inspect ({fields:fields, files:files})); }); });/*supervisor./bin/www*/Module.exports= Router;
<!DOCTYPE HTML><HTML><Head><title><%=title%></title><Linkrel= ' stylesheet 'href= '/stylesheets/style.css '/></Head><Body><h5>Here the variables are not output but not reimbursed:<%=Locals.title%></h5><P>Welcome to<%=title%></P><imgsrc= './images/<%=Locals.imginfo%>' width= '/><formMethod= "POST"enctype= "Multipart/form-data"Action= "/file-upload"> <inputtype= "text"name= "username"> <inputtype= "Password"name= "Password"> <inputtype= "File"name= "thumbnail"> <inputtype= "Submit"></form></Body></HTML>
Change file name after node Express formidable file upload