There are two scenarios: the two options are different
Upload attachments in Nodejs call the middleware of Multer, use this middleware to upload
The first is the form (the front-end section):
<! DOCTYPE html>
Server-side:
varExpress = require ('Express');varRouter =Express. Router ();varMulter = require ('Multer');varFS = require ('FS');ConstUpload_path ='./uploads'varUpload =multer ({dest:upload_path})/*GET home page.*/router.Get('/', Function (req, res, next) {Res.render ('Index', {title:'Express' });});//multiple file uploads//router.post ('/upload ', Upload.array (' fileUpload '), function (req, res, next) {//const FILES = req.files;//Const RESPONSE = [];//Const RESULT = new Promise ((Resolve, reject) + = {//Files.map ((v) + = {//fs.readfile (V.path, function (err, data) {//fs.writefile (' ${upload_path}/${v.originalname} ', data, function (err, data) {//Const result = {//File:v,// }//if (err) reject (err);//Resolve (' success ');// })// })// })// })//Result.then (r = {//Res.json ({//msg: ' Upload succeeded ',// })//}). catch (Err = {//Res.json ({err})// });// })//Single File UploadRouter.post ('/upload', Upload.single ('FileUpload'), function (req, res, next) {Const{File} =req; Console.log (Req.files); Fs.readfile (File.path, function (err, data) {Fs.writefile (' ${upload_path}/${file.originalname} ', data, function (ERR) {if(Err) Res.json ({err}) Res.json ({msg:'Upload Successful' }); }); })}) Module.exports= Router;There's another way to upload this form is to upload it asynchronously (one of my more common ones).
This would require godless of the transmitted data (first encapsulating the resulting data as formdata and then back to the backend)
Let _this = this; document.getElementById (' upload '). onchange = function (e) {let file = E.target.files[0]; var formData = new FormData (); Formdata.append (' fileUpload ', file); _this.axios.post ('/user/file-upload ', formData). Then (function (response) { if (response.data.state = = 200) { _this.imageurl = _this. $store. State.imgbaseurl + response.data.result; _this.registerfromdata.imageurl = Response.data.result;}} ); }
Nodejs Uploading Attachments