1

I am trying to upload some pics using a node-express server and I am facing two issues, one is regarding the picture extension and the other one regarding the picture itself being displayed. So after I am uploading the file is added into the upload path but a kind like this auto-generated name 2f22c2502b907f7bf0bc2567c43c801cwithout extension like .png and even if I'm renaming the file like 2f22c2502b907f7bf0bc2567c43c801c.png in browser is looking like default empty icon browser instead of uploaded picture.

How can I solve those two issues ?

const express = require('express')
const multer  = require('multer')
const path = require('path')
const child_process = require("child_process");
const app = express();
const PORT = 3000;
const upload = multer({ dest: 'uploads/' })

  app.post('/upload', upload.single('picture'), function (req, res, next) {
    const FILE_OUTPUT = path.join(__dirname, `uploads/${req.file.path}`)

  child_process.execFile(
      "/usr/bin/convert",
      [path.join(__dirname, req.file.path), "-resize", "280x150", FILE_OUTPUT],
      function() {
        console.log('done resizing', FILE_OUTPUT)
        return res.send(`

                <img src="${FILE_OUTPUT}"/>
     
          `);
      }
    );
});

app.use('/', express.static(path.join(__dirname, 'public')));

app.listen(PORT, function () { console.log('Example app listening on port: ', PORT) })
RulerNature
  • 693
  • 4
  • 13
  • 40

1 Answers1

1

That should be a multer task because multer will not append the file extension so what need to be done is a bit of setup on multer.

Check this response multer setup

BurebistaRuler
  • 6,209
  • 11
  • 48
  • 66