HI WELCOME TO SIRIS

Express.js File Upload

Leave a Comment
In Express.js, file upload is slightly difficult because of its asynchronous nature and networking approach.
It can be done by using middleware to handle multipart/form data. There are many middleware that can be used like multer, connect, body-parser etc.
Let's take an example to demonstrate file upload in Node.js. Here, we are using the middleware 'multer'.
Create a folder "jtp file upload" having the following files:
Express.js file upload 1
uploads: It is an empty folder i.e. created to store the uploaded images.
package: It is JSON file, having the following data:
File: package.json
  1. {  
  2.   "name": "file_upload",  
  3.   "version": "0.0.1",  
  4.   "dependencies": {  
  5.     "express": "4.13.3",  
  6.     "multer": "1.1.0"  
  7.   },  
  8.   "devDependencies": {  
  9.     "should": "~7.1.0",  
  10.     "mocha": "~2.3.3",  
  11.     "supertest": "~1.1.0"  
  12.   }  
  13. }  
File: index.html
  1. <html>  
  2.   <head>  
  3.     <title>File upload in Node.js by Javatpoint</title>  
  4.  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>  
  5.   <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js"></script>  
  6.   <script>  
  7.   $(document).ready(function() {  
  8.      $('#uploadForm').submit(function() {  
  9.          $("#status").empty().text("File is uploading...");  
  10.   
  11.         $(this).ajaxSubmit({  
  12.   
  13.             error: function(xhr) {  
  14.                     status('Error: ' + xhr.status);  
  15.             },  
  16.   
  17.             success: function(response) {  
  18.                     console.log(response)  
  19.                     $("#status").empty().text(response);  
  20.             }  
  21.     });  
  22.   
  23.     return false;  
  24.     });      
  25. });  
  26.   </script>  
  27.   </head>  
  28.   <body>  
  29.       <h1>Express.js File Upload: by Javatpoint</h1>  
  30.       <form id="uploadForm" enctype="multipart/form-data" action="/uploadjavatpoint" method="post">  
  31.        <input type="file" name="myfile" /><br/><br/>  
  32.        <input type="submit" value="Upload Image" name="submit"><br/><br/>  
  33.        <span id="status"></span>  
  34.       </form>  
  35.   </body>  
  36. </html>  
File: server.js
  1.    
  2. var express =   require("express");  
  3. var multer  =   require('multer');  
  4. var app =   express();  
  5. var storage =   multer.diskStorage({  
  6.   destination: function (req, file, callback) {  
  7.     callback(null'./uploads');  
  8.   },  
  9.   filename: function (req, file, callback) {  
  10.     callback(null, file.originalname);  
  11.   }  
  12. });  
  13. var upload = multer({ storage : storage}).single('myfile');  
  14.   
  15. app.get('/',function(req,res){  
  16.       res.sendFile(__dirname + "/index.html");  
  17. });  
  18.   
  19. app.post('/uploadjavatpoint',function(req,res){  
  20.     upload(req,res,function(err) {  
  21.         if(err) {  
  22.             return res.end("Error uploading file.");  
  23.         }  
  24.         res.end("File is uploaded successfully!");  
  25.     });  
  26. });  
  27.   
  28. app.listen(2000,function(){  
  29.     console.log("Server is running on port 2000");  
  30. });  
To install the package.json, execute the following code:
  1. npm install  
Express.js file upload 2
It will create a new folder "node_modules" inside the "jtp file upload" folder.
Express.js file upload 3
Dependencies are installed. Now, run the server:
  1. node server.js  
Express.js file upload 4
Open the local page http://127.0.0.1:2000/ to upload the images.
Express.js file upload 5
Select an image to upload and click on "Upload Image" button.
Express.js file upload 6
Here, you see that file is uploaded successfully. You can see the uploaded file in the "Uploads" folder.
Express.js file upload 7

Download Node.js Express File Upload Example

Download this example

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.