Search code examples
node.jsmongodbexpressfile-uploadmulter

How to upload file within Express API callback?


I know how I can upload a file from a client to an Express server using multer.

However, this can only be done using a middle ware function.

What I am looking for is a way to upload files from within the callback of the Express API.

My use case is this:

  1. Client uploads a CSV file having URLs of images.

  2. I will download those images from given URLs

  3. Then I will upload those images in MongoDb within Express API callback

( I am looking for how to do the 3rd step. )

Something like this:

app.post('/uploadtoDB',multer.single('file'),(req,res)=>{

    let urls = parseCSV_and_GetUrls(req.file);
    urls.forEach((url)=>{
         downloadImage(url)
    });

    //Scan directory to get downloaded images 
    let stream = fs.createReadStream('/path/to/downloadedFile');

    //Upload the downloaded image to MongoDb
    uploadfile_to_MongoDB(stream); 
})

Solution

  • I would not recommend storing images directly in MongoDB. It is advisible to store them on a static filestore (S3) and just keep the url/reference to it in MongoDB.

    If you really want to store it in MongoDB, you could convert the image to a base64-encoded string. Note that with this method you need to make sure that the resulting document is below 16 MB as this the max. allowed doc size.

    const fs = require('fs');
    
    async function storeFileAsBase64EncodedString(pathToFile) {    
       const buffer = await fs.promises.readFile(pathToFile);    
       await db.collection('yourCollection').insertOne({ image: buffer.toString('base64') });    
    }
    

    Another option is to use GridFs, see this for further details.