-3

I am working on a project and i need to upload and save image to MongoDB. I using NodeJS as backend and Angular Material for frontend. I am writing the node and angular in TypeScript. How this upload and save can happen. I want also to know how I can reed and display them.

1 Answers1

0

Check Multer

example of use

store.js

import multer from 'multer';

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        const path = process.cwd() + '\\uploads';
        cb(null, path);
    },
    filename: function (req, file, cb) {
        cb(null, `file-${Date.now()}.${file.originalname.split(/[.]+/).pop()}`);
    }
});


export const UploadHandler = multer({
    storage: storage,
    fileFilter(req, file, callback, acceptFile) {
        if (['image/png'].indexOf(file.mimetype) === -1) {
            return callback(new Error("This File Is Not Supported"), false);
        }
        return callback(null, true);
    }
});

app.js

import store from './store';

app.post('/upload', store.array('files', 1), function (req, res, next) {
    if(req.files && req.files.length > 0) {
        // files uploaded successfully
    }
});
Jamal Abo
  • 472
  • 4
  • 16