App.js introduces the third-party middleware Multer to implement the file upload function.
var multer = require (' Multer '); Use Express's third-party middleware Multer to implement the file upload function. App.use (Multer ({// settings file uploaded to location dest: './public/images/upload ', function (fieldname, filename) { return filename; }});
Routing
function (req, res) { Req.flash (' success ', ' File upload succeeded! ') ); Res.json ({"result": {message: "File Upload succeeded!") }}); });
Html
<type= "file" ID= "Photo">< ID = "Upload" > upload </button>
Ajax
/*Uploading Files*/ $("#upload"). Click (function(){ //Do somethingvar file = $ ("#photo") [0].files; //Last Modified TimeConsole.log ("File Last Modified" +file[0].lastmodifieddate); //nameConsole.log ("File name" +file[0].name); //size byteConsole.log ("File Size" +file[0].size); //typeConsole.log ("File type" +file[0].type); //Create a Formdata object vardata =NewFormData (); //adding data to a Formdata object$.each (file,function(i, file) {Data.append (' Upload_file ', file); }); $.ajax ({data:data, url:'/upload ', type:"POST", DataType:' JSON ', ContentType:false,//not missingProcessData:false,//not missingSuccessfunction(data) {Console.log (data); Alert ("Upload success!" "); }, Error:function(Err) {Console.log (' ERROR: ' +err); } }); })
Nodejs+ajax enables simple file upload functionality