yandex
loader

please wait

  • Kavya Mathur Dec-24-2018 05:59:40 AM ( 3 months ago )

    which dependency to install for appending a form data to server ?

  • Vikrant Srivastava Dec-24-2018 06:07:15 AM ( 3 months ago )

    Multer

    app.use(express.static('public')); 

    app.use('/scripts', express.static(`${__dirname}/node_modules/`));

    app.use(bodyParser.json({limit: '50mb'}))

    app.use(bodyParser.urlencoded({limit: '50mb', extended: false}))

    // Redirect all traffic to index.html

    app.get('/',function (req, res){ 

    res.sendFile(`${__dirname}/public/index.html`)

     

    });

     

    app.post('/test', function(req, res){

       var upload = multer({

            storage: multer.memoryStorage()

        }).single('file')

        upload(req, res, function(err) {

            var buffer = req.file.buffer;

           // var magic = buffer.toString('hex', 0, 4)

            var filename = req.file.fieldname + '-' + Date.now() + path.extname(req.file.originalname)

            

                fs.writeFile('./uploads/' + filename, buffer, 'binary', function(err) {

                    if (err) throw err

                    res.send('File is uploaded')

                })

            /* } else {

                res.end('File is no valid')

            } */

        })

    });

Please login

Similar Discussion

Recommended For You