how to upload image file and display using express nodejs

前端 未结 5 1141
悲&欢浪女
悲&欢浪女 2020-12-13 11:14

I have used following code :

fileupload.html



File Uploading Form


<         


        
5条回答
  •  生来不讨喜
    2020-12-13 11:40

    It seems that it's not safe to keep original file name or extension and I found/made this code for displaying images directly everywhere by adding .jpg to your file url:

    restapi.get('/uploads/:id', function (req, res) {
      var storedMimeType = 'image/jpeg';
      res.setHeader('Content-Type', storedMimeType)
     fs.createReadStream(path.join('./uploads/', req.params.id).replace(/\.[^/.]+$/, "")).pipe(res)
    })
    

提交回复
热议问题