{"id":946,"date":"2024-04-14T14:16:10","date_gmt":"2024-04-14T14:16:10","guid":{"rendered":"https:\/\/www.appfinz.com\/blogs\/?p=946"},"modified":"2025-05-15T04:48:57","modified_gmt":"2025-05-15T04:48:57","slug":"angular-89-node-express-js-file-upload-tutorial","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/angular-89-node-express-js-file-upload-tutorial\/","title":{"rendered":"Angular 8|9 Node &amp; Express JS File Upload Tutorial"},"content":{"rendered":"\n<p>In this Angular 8|9 and Node.js blog series, we are going to look at&nbsp;<strong>how to upload files on the Node server<\/strong>. To&nbsp;<strong>create an Angular image upload component<\/strong>, I am going to use Angular 8|9 front-end framework along with&nbsp;ng2-file-upload&nbsp;NPM package; It\u2019s an easy to use Angular directives for uploading the files.<\/p>\n\n\n\n<p>I am also going to take the help of Node.js to create the backend server for Image or File uploading demo. Initially, we\u2019ll set up an Angular 8|9 web app from scratch using Angular CLI. You must have Node.js and Angular CLI installed in your system.&nbsp;<a href=\"https:\/\/appfinz.com\/blogs\/wp-content\/uploads\/2019\/12\/angular-node-file-upload-master.zip\">Source Code<\/a>&nbsp;is also attached with this section you can download it and use it anywhere in your projects<\/p>\n\n\n\n<p>So lets create the local server using Node.js and multer middleware.&nbsp;<a href=\"https:\/\/www.npmjs.com\/package\/multer\" target=\"_blank\" rel=\"noreferrer noopener\">Multer<\/a>&nbsp;is a node.js middleware for handling multipart\/form-data, which is primarily used for uploading files. Once we are done setting up front-end and backend for our File uploading demo then, we\u2019ll see step by step how to configure file uploading in Angular app with the help of Node js server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisite<\/h3>\n\n\n\n<p>In order to show you Angular 8|9 File upload demo,I am assuming that you should have Node.js and Angular CLI installed in your system. If not then download&nbsp;<a href=\"https:\/\/nodejs.org\/\">Node js<\/a>&nbsp;Here<\/p>\n\n\n\n<p>After Installing Node js Now Run the following the command to install Angular CLI. It will install angular cli on your system globally<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\"><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Install Angular App<\/h3>\n\n\n\n<p>Now Run the command to install the Angular project:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">ng new angular-node-file-upload\n\n# ? Would you like to add Angular routing? No\n# ? Which stylesheet format would you like to use? CSS<\/pre><\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">cd angular-node-file-upload<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Show Alert Messages When File Uploaded<\/h3>\n\n\n\n<p>We are going to install and configure&nbsp;<a href=\"https:\/\/www.npmjs.com\/package\/ngx-toastr\" target=\"_blank\" rel=\"noreferrer noopener\">ngx-toastr<\/a>&nbsp;an NPM package which helps in showing the alert message when the file is uploaded on the node server.so you can also follow the same procedure<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">npm install ngx-toastr --save<\/pre><\/div>\n\n\n\n<p>The&nbsp;<strong>ngx-toastr<\/strong>&nbsp;NPM module requires&nbsp;<strong>@angular\/animations<\/strong>&nbsp;dependency:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">npm install @angular\/animations --save<\/pre><\/div>\n\n\n\n<p>Then, add the&nbsp;<strong>ngx-toastr<\/strong>&nbsp;CSS in&nbsp;<code>angular.json<\/code>&nbsp;file:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&quot;styles&quot;: [\n    &quot;src\/styles.css&quot;,\n    &quot;node_modules\/ngx-toastr\/toastr.css&quot;\n]<\/pre><\/div>\n\n\n\n<p>Import&nbsp;<strong>BrowserAnimationsModule<\/strong>&nbsp;and&nbsp;<strong>ToastrModule<\/strong>&nbsp;in&nbsp;<code>app.module.ts<\/code>&nbsp;file:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">import { BrowserAnimationsModule } from '@angular\/platform-browser\/animations';\nimport { ToastrModule } from 'ngx-toastr';\n \n@NgModule({\n  imports: [\n    CommonModule,\n    BrowserAnimationsModule, \/\/ required animations module\n    ToastrModule.forRoot() \/\/ ToastrModule added\n  ]\n})\n\nexport class AppModule { }<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Install &amp; Configure ng-file-upload Directive<\/h3>\n\n\n\n<p>In this step, we\u2019ll Install and configure ng-file-upload library in Angular 8|9 app. Run command to install&nbsp;the&nbsp;<strong>ng-file-upload<\/strong>&nbsp;library.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">npm install ng2-file-upload<\/pre><\/div>\n\n\n\n<p>Once the&nbsp;<strong>ng2-file-upload<\/strong>&nbsp;directive is installed, then import the&nbsp;<strong>FileSelectDirective<\/strong>&nbsp;and&nbsp;<strong>FormsModule<\/strong>&nbsp;in&nbsp;<code>app.module.ts<\/code>. We need&nbsp;<strong>FormsModule<\/strong>&nbsp;service so that we can create the file uploading component in Angular.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">import { FileSelectDirective } from 'ng2-file-upload';\nimport { FormsModule } from '@angular\/forms';\n\n@NgModule({\n  declarations: [\n    FileSelectDirective\n  ],\n  imports: [\n    FormsModule\n  ]\n})\n\nexport class AppModule { }<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">After that We will setting Up Node Backend for File Upload Demo<\/h3>\n\n\n\n<p>To upload the file on the server, we need to set up a separate backend. In this blog series, we will be using Node &amp; Express js to create server locally along with&nbsp;<strong>multer, express js, body-parser, and dotenv<\/strong>&nbsp;libraries.<\/p>\n\n\n\n<p>Run command to create backend folder in Angular app\u2019s root directory:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">mkdir backend &amp;&amp; cd backend<\/pre><\/div>\n\n\n\n<p>In the next step, create a specific&nbsp;<code>package.json<\/code>&nbsp;file.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">npm init<\/pre><\/div>\n\n\n\n<p>Run command to install required dependencies:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">npm install express cors body-parser multer dotenv --save<\/pre><\/div>\n\n\n\n<p>In order to get rid of starting the server again and again, install nodemon NPM package. Use \u2013<code>-save-dev<\/code>&nbsp;along with the npm command to register in the&nbsp;<strong>devDependencies<\/strong>&nbsp;array. It will make it available for development purpose only.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">npm install nodemon --save-dev<\/pre><\/div>\n\n\n\n<p>Have a look at final&nbsp;<strong>pacakge.json<\/strong>&nbsp;file for file upload demo backend:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">{\n  &quot;name&quot;: &quot;angular-node-file-upload&quot;,\n  &quot;version&quot;: &quot;1.0.0&quot;,\n  &quot;description&quot;: &quot;Angualr 8 file upload demo app&quot;,\n  &quot;main&quot;: &quot;server.js&quot;,\n  &quot;scripts&quot;: {\n    &quot;test&quot;: &quot;echo \\&quot;Error: no test specified\\&quot; &amp;&amp; exit 1&quot;,\n    &quot;start&quot;: &quot;node server.js&quot;\n  },\n  &quot;author&quot;: &quot;Digamber Rawat&quot;,\n  &quot;license&quot;: &quot;ISC&quot;,\n  &quot;dependencies&quot;: {\n    &quot;body-parser&quot;: &quot;^1.19.0&quot;,\n    &quot;cors&quot;: &quot;^2.8.5&quot;,\n    &quot;dotenv&quot;: &quot;^8.0.0&quot;,\n    &quot;express&quot;: &quot;^4.17.1&quot;,\n    &quot;multer&quot;: &quot;^1.4.1&quot;\n  },\n  &quot;devDependencies&quot;: {\n    &quot;nodemon&quot;: &quot;^1.19.1&quot;\n  }\n}<\/pre><\/div>\n\n\n\n<p>Create a file by the name of&nbsp;<code>`server.js`<\/code>&nbsp;inside&nbsp;<code>`backend`<\/code>&nbsp;folder:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Configure Server.js<\/h3>\n\n\n\n<p>To configure our backend we need to create a&nbsp;<code>server.js<\/code>&nbsp;file. In this file, we\u2019ll keep our backend server\u2019s settings.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">touch server.js<\/pre><\/div>\n\n\n\n<p>Now, paste the following code in&nbsp;<code>backend &gt; server.js<\/code>&nbsp;file:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">const express = require('express'),\n  path = require('path'),\n  cors = require('cors'),\n  multer = require('multer'),\n  bodyParser = require('body-parser');\n\n\/\/ File upload settings  \nconst PATH = '.\/uploads';\n\nlet storage = multer.diskStorage({\n  destination: (req, file, cb) =&gt; {\n    cb(null, PATH);\n  },\n  filename: (req, file, cb) =&gt; {\n    cb(null, file.fieldname + '-' + Date.now())\n  }\n});\n\nlet upload = multer({\n  storage: storage\n});\n\n\/\/ Express settings\nconst app = express();\napp.use(cors());\napp.use(bodyParser.json());\napp.use(bodyParser.urlencoded({\n  extended: false\n}));\n\napp.get('\/api', function (req, res) {\n  res.end('File catcher');\n});\n\n\/\/ POST File\napp.post('\/api\/upload', upload.single('image'), function (req, res) {\n  if (!req.file) {\n    console.log(&quot;No file is available!&quot;);\n    return res.send({\n      success: false\n    });\n\n  } else {\n    console.log('File is available!');\n    return res.send({\n      success: true\n    })\n  }\n});\n\n\/\/ Create PORT\nconst PORT = process.env.PORT || 8080;\nconst server = app.listen(PORT, () =&gt; {\n  console.log('Connected to port ' + PORT)\n})\n\n\/\/ Find 404 and hand over to error handler\napp.use((req, res, next) =&gt; {\n  next(createError(404));\n});\n\n\/\/ error handler\napp.use(function (err, req, res, next) {\n  console.error(err.message);\n  if (!err.statusCode) err.statusCode = 500;\n  res.status(err.statusCode).send(err.message);\n});<\/pre><\/div>\n\n\n\n<p>Now, while staying in the backend folder run the below command to start the backend server:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">nodemon server.js<\/pre><\/div>\n\n\n\n<p>If everything goes fine then you\u2019ll get the following output:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">[nodemon] 1.19.1\n[nodemon] to restart at any time, enter `rs`\n[nodemon] watching: *.*\n[nodemon] starting `node server.js`\nConnected to port 8080<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Create an Angular image upload component<\/strong><\/h2>\n\n\n\n<p>In this last step, we are going to create a file upload component in an Angular app using Express js API.<\/p>\n\n\n\n<p>Get into the&nbsp;<code>app.component.ts<\/code>&nbsp;file and include the following code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">import { Component, OnInit } from '@angular\/core';\nimport { FileUploader } from 'ng2-file-upload\/ng2-file-upload';\nimport { ToastrService } from 'ngx-toastr';\n\nconst URL = 'http:\/\/localhost:8080\/api\/upload';\n\n@Component({\n  selector: 'app-root',\n  templateUrl: '.\/app.component.html',\n  styleUrls: ['.\/app.component.css']\n})\n\nexport class AppComponent implements OnInit {\n  public uploader: FileUploader = new FileUploader({\n    url: URL,\n    itemAlias: 'image'\n  });\n\n  constructor(private toastr: ToastrService) { }\n\n  ngOnInit() {\n    this.uploader.onAfterAddingFile = (file) =&gt; {\n      file.withCredentials = false;\n    };\n    this.uploader.onCompleteItem = (item: any, status: any) =&gt; {\n      console.log('Uploaded File Details:', item);\n      this.toastr.success('File successfully uploaded!');\n    };\n  }\n\n}<\/pre><\/div>\n\n\n\n<p>Go to&nbsp;<code>app.component.html<\/code>&nbsp;file and add the given below code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;div class=&quot;wrapper&quot;&gt;\n  &lt;h2&gt;Angular Image Upload Demo&lt;\/h2&gt;\n\n  &lt;div class=&quot;file-upload&quot;&gt;\n    &lt;input type=&quot;file&quot; name=&quot;image&quot; ng2FileSelect [uploader]=&quot;uploader&quot; accept=&quot;image\/x-png,image\/gif,image\/jpeg&quot; \/&gt;\n    &lt;button type=&quot;button&quot; (click)=&quot;uploader.uploadAll()&quot; [disabled]=&quot;!uploader.getNotUploadedItems().length&quot;&gt;\n      Upload\n    &lt;\/button&gt;\n  &lt;\/div&gt;\n\n&lt;\/div&gt;<\/pre><\/div>\n\n\n\n<p>Now, It\u2019s time to start the Angular 8|9 app to check out the&nbsp;<strong>File upload demo<\/strong>&nbsp;in the browser. Run the following command:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">ng serve --open<\/pre><\/div>\n\n\n\n<p>When you upload the image from front-end you\u2019ll see your image files are saving inside the&nbsp;<code>backend &gt; uploads<\/code>&nbsp;folder.<\/p>\n\n\n\n<p>To compare your code you can check out the below download section:<\/p>\n\n\n\n<p>Must check out the following tutorial on&nbsp;how to show image preview in Angular before uploading.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>In this&nbsp;Angular 8|9 Blog Series, we barely scratched the surface related to file uploading in a Node application. There are various other methods available on the internet through which you can achieve file uploading task quickly. However, this Blog is suitable for beginners developers. I hope this blog will surely help and you if you liked this tutorial, please consider sharing it with others.<\/p>\n\n\n\n<p>If you\u2019re planning to revamp your online presence, don\u2019t miss exploring our <a href=\"https:\/\/www.appfinz.com\/website-designing-company-in-delhi\">website designing services<\/a> and <a href=\"https:\/\/www.appfinz.com\/website-development-company-in-delhi\">custom website development solutions<\/a> tailored to your business needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Angular 8|9 and Node.js blog series, we are going to look at&nbsp;how to<\/p>\n","protected":false},"author":1,"featured_media":1280,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-946","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angularjs"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=946"}],"version-history":[{"count":3,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/946\/revisions"}],"predecessor-version":[{"id":1343,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/946\/revisions\/1343"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media\/1280"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}