Skip to content Skip to sidebar Skip to footer

Extract File From Post Request Nodejs

I am trying nodejs for the first time. I am using it with python shell. I am trying to transfer a file from one PC to another using Post request app.js (Server PC) app.use(bodyPars

Solution 1:

I'm going to guess and say you're using Express based on the syntax in your question. Express doesn't ship with out of the box support for file uploading.

You can use the multer or busboy middleware packages to add multipart upload support.

Its actually pretty easy to do this, here is a sample with multer

const express = require('express')
const bodyParser = require('body-parser')
const multer = require('multer')

const server = express()
const port = process.env.PORT || 1337// Create a multer upload directory called 'tmp' within your __dirnameconst upload = multer({dest: 'tmp'})

server.use(bodyParser.json())
server.use(bodyParser.urlencoded({extended: true}))

// For this route, use the upload.array() middleware function to // parse the multipart upload and add the files to a req.files array
server.port('/mytestapp', upload.array('files') (req, res) => {
    // req.files will now contain an array of files uploaded console.log(req.files)
})

server.listen(port, () => {
    console.log(`Listening on ${port}`)
})

Post a Comment for "Extract File From Post Request Nodejs"