Node.js Implementation of file upload

The file is referred to in the file is to upload the user’s local file to the server.

Upload file needs to handle two locations:

    How does the client upload file?
  1. The form item of the upload file needs to be specified as INPUT, TYPE is file
  2. To upload the file must set the form ENCTYPE to Multipart / Form-Data to indicate that the form will be in multiple parts form Upload ENCTYPE = “Application / X-WWW-FORM-URLENCODED” is the default. The meaning of this value refers to the URL encoding of the form of the form, and the so-called URL encoding converts the request parameters to binary encoding.
  3. 1, Multipart / Form-Data Body-Parser Unable to resolve, Multer Module is specifically used to help us analyze the multi-parts.


File UPLOAD
 Hi Sir  Upupup  

// Using the AJAX upload file
 var btn = document.getlementById ("btn"); btn.onclick = function () {// FormData This object can be used to perform data in the form Package // Use FormData to package VAR fd = new formData ("FORM")); // Create an XHR objectVar xhr = new xmlhtpRequest (); // Set the address xhr.open ("post", "/ testupload"); // Send request xhr.send (FD); // Receive Response Xhr.onReadyStateChange = function () {IF (xhr.readyState == 4 && xhr.status == 200) {alert (xhr.responsetext);}}; // Cancel default behavior returnaf false;};    
2. Server-side
const express = Require (“express”); const app = expression (); const bodyparser = required “Body-Parser”); const multer = Require (“MULTER”); app.use ({extended: false}); app.use (bodyparser.json ()); app.use Static (“public”); const storage = multer.diskStorage ({// Destination) The path to the upload file can receive a callback function, or a string // If a callback function is passed, it is necessary to ensure that the path is effective. Destination: ‘uploads /’, // filename properties can be used to specify filesSave it to the name of the name FileName: Function (REQ, FILE, CB) {// CB (NULL, File, CB) {// CB (NULL, FILE.FIELDNAME + – ‘+ Date.Now ()) // Chrysanthemum. JPG let fname = file.ORIGINALNAME; let extName = “”; // determines if the file has an extension IF (“fname.lastIndexof (“)! = -1) {extname = fname.slice (“..lastIndexof (“. “));} // Upload files, generally not directly saving the user’s file name directly // generally generates a file name CB (null, file.fieldname +” + Date.Now ( ) + extname);}}) const upload = MULTER ({Storage: Storage, Limits: {// Limit file size is 200KB FileSize: 1024 * 200}}); app.post (“/ testupload”, (Req, RES) => {// Get the function let fu = upload.single of the resolution request body; / / manually call the function to resolve the requester FU (Req, Res, (ERR) => {IF (ERR) {// Try to pass the error, the file size exceeds the standard res. extend, please upload the file below 200KB !! “);} Else {// Read Request Parameter Let usErname = req.body.username; res. extend ~~~ “);}});}); app.listen (3333, () => {Console.log (” Success “);})

   
The above is all the content of this article, I hope to learn from everyone Helped, I also hope that everyone will support Tumi Clouds.
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment