Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

Express js upload and save base64 image into File System

const express = require('express')
const app = express()
const port = 6000
const bodyParser = require('body-parser');
const fs = require('fs');

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json

const uploadImage = async (req, res, next) => {

    try {

        // to declare some path to store your converted image
        const path = './images/'+Date.now()+'.png'

        const imgdata = req.body.base64image;

        // to convert base64 format into random filename
        const base64Data = imgdata.replace(/^data:([A-Za-z-+/]+);base64,/, '');
        fs.writeFileSync(path, base64Data,  {encoding: 'base64'});

        return res.send(path);

    } catch (e) {

app.post('/upload/image', uploadImage)

app.listen(port, () => console.log(`Example app listening on port ${port}!`))
  1. i want to create a gallery admin pannel using nodejs .
    i want to store the data in mysql in base64 string .
    and display the images within the same page along with a delete button which delete the image data from mysql table.
    please guide

Leave a Reply