Change file name after node Express formidable file upload

Source: Internet
Author: User

//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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.