In this tutorial, I will explain how to upload images in Angular 2. In this demo, I am using Angular 2 as front-end and node.js (with express template) as back-end along with MySQL as database. I am using Angular-cli to create the project. I hope you guys have already created the project using Angular-cli. If not, you can find how to create one.
You can also find both back-end and front-end links here.
Note
To run both projects, you need to install dependency from the package.json. You can simple do that by navigating to the project directory and writing the command "npm install".
Then, to run the back-end project, write "npm start"
Setting up the table in MySQL
- script file
- for student_tbl[code language = "sql"]
- CREATE TABLE IF NOT EXISTS `student_tbl` (`rno`
- int(11) NOT NULL, `name`
- varchar(50) DEFAULT NULL, `mobile_no`
- varchar(15) DEFAULT NULL, `student_img`
- varchar(1000) NOT NULL, PRIMARY KEY(`rno`)) ENGINE = InnoDB DEFAULT CHARSET = latin1;
Back-end using node.js
First, I will start from creating the back-end using node.js. If you want to know the steps for how to create REST API using node.js, you can go here.
So first, I will create dbconnection.js file which will store the hostname, username, and database name.
- var mysql = require('mysql');
- var connection = mysql.createPool({
- host: 'localhost',
- user: 'root',
- password: '',
- database: 'demo'
- });
- module.exports = connection;
Now, in the next step, I will create the student_model inside Models directory. It contains all the methods for fetching, inserting, and deleting the student records from the database.
- var db = require('../dbconnection');
- var fs = require('fs');
- var Student = {
- getAllStudent: function(callback) {
- return db.query("select * from student_tbl", callback);
- },
- deleteStudent: function(Student, callback) {
- if (Student.student_img != '') {
- var path = './public' + Student.student_img;
- fs.unlink(path, function(err) {
- if (err) {
- console.log(err);
- }
- console.log('Deleted successfuly')
- });
- }
- return db.query("delete from student_tbl where rno=?", [Student.rno], callback);
- },
- addStudent: function(Student, callback) {
- var dt = new Date(); //current date and time of server
- var text = ""; //random text
- var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length));
- var base64d = Student.student_img.replace(/^data:image\/png;base64,/, "");
- var path = "./public/images/" + text + dt.getDate() + dt.getMonth() + dt.getMilliseconds() + ".png";
- var path1 = "/images/" + text + dt.getDate() + dt.getMonth() + dt.getMilliseconds() + ".png";
- fs.writeFile(path, base64d, 'base64', function(err) {
- if (err) {
- return console.log(err);
- }
- console.log("The file was saved!");
- });
- return db.query("Insert into student_tbl values(?,?,?,?)", [Student.rno, Student.name, Student.mobile_no, path1], callback);
- }
- };
- module.exports = Student;
First, I need to import dbconnection.js and file system references to student_model.js file. In the above code, the first method "getAllStudent" simply returns all the records from the student_tbl.
Now, set the routes in app.js.
- var express = require('express');
- var path = require('path');
- var favicon = require('serve-favicon');
- var logger = require('morgan');
- var cookieParser = require('cookie-parser');
- var bodyParser = require('body-parser');
- var cors = require('cors');
- var index = require('./routes/index');
- var users = require('./routes/users');
- var Students = require('./routes/Students');
- var app = express();
- // view engine setup
- app.set('views', path.join(__dirname, 'views'));
- app.set('view engine', 'jade');
- // uncomment after placing your favicon in /public
- //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
- app.use(cors());
- app.use(logger('dev'));
- app.use(bodyParser.json({
- limit: '50mb'
- }));
- app.use(bodyParser.urlencoded({
- limit: '50mb',
- extended: false
- }));
- app.use(cookieParser());
- app.use(express.static(path.join(__dirname, 'public')));
- app.use('/', index);
- app.use('/users', users);
- app.use('/Students', Students);
- // catch 404 and forward to error handler
- app.use(function(req, res, next) {
- var err = new Error('Not Found');
- err.status = 404;
- next(err);
- });
- // error handler
- app.use(function(err, req, res, next) {
- // set locals, only providing error in development
- res.locals.message = err.message;
- res.locals.error = req.app.get('env') === 'development' ? err : {};
- // render the error page
- res.status(err.status || 500);
- res.render('error');
- });
- module.exports = app;
Now, one important thing in app.js is that I am defining the limit of data. By default, it is set to 1 MB. So, I have extended it to 50 MB.
Front-end using Angular 2
I will start from creating data structure for student.
student.ts
- export class Student {
- public constructor(public rno: number, public name: string, public mobile_no: string, public student_img: string) {}
- }
I have created one class for students which contains 4 columns like rno, name, mobile_no, and student_img.
Now, I will create a component to add a new student.
addstudent.component.html
- <div class="container">
- <form (ngSubmit)="studentSubmit()" #addform="ngForm">
- <div class="form-group"> <label for="id">rollno</label> <input type="number" [(ngModel)]="model.rno" name="rno" class="form-control" id="rno" required #rno="ngModel"> </div>
- <div [hidden]="rno.valid || rno.pristine" class="alert alert-danger"> Roll No is required </div>
- <div class="form-group"> <label for="name">Name</label> <input type="text" [(ngModel)]="model.name" name="name" class="form-control" id="name" required #name="ngModel"> </div>
- <div [hidden]="name.valid || name.pristine" class="alert alert-danger"> Student Name is required </div>
- <div class="form-group"> <label for="name">Mobile Number</label> <input type="text" [(ngModel)]="model.mobile_no" name="mobile_no" class="form-control" id="mobile_no" required #mobile_no="ngModel"> </div>
- <div [hidden]="mobile_no.valid || mobile_no.pristine" class="alert alert-danger"> Student Mobile Number is required </div>
- <div class="form-group"> <input type="file" name="student_img" required (change)="fileChange(input)" #input /> </div>
- <div> <img [attr.src]='file_srcs[0]' alt="" /> </div> <button type="submit" class="btn btn-default form-control" [disabled]="!addform.form.valid">Add Task</button> </form>
- </div>



First LastPosted Nov 14, 2019, 3:40 PM
At add time, how to store the image into a database table as a column: [ImageData] [varbinary](max) NOT NULL. Not into a folder.
PoojaPosted Sep 30, 2017, 10:29 AM
Does front end work if in backend java is used