Nodejs Uploading Attachments

Source: Internet
Author: User

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

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.