Skip to content

Instantly share code, notes, and snippets.

@HarshithaKP
Created November 19, 2019 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HarshithaKP/ebc0e79800e5638fe827c157360378be to your computer and use it in GitHub Desktop.
Save HarshithaKP/ebc0e79800e5638fe827c157360378be to your computer and use it in GitHub Desktop.
Multer file upload with Axios client
var axios = require('axios')
var FormData = require('form-data')
var fs = require('fs')
const form = new FormData()
const url = 'http://localhost:8000'
//Read the file from disc
form.append ('file',fs.createReadStream('./test.js'))
axios.post (url, form, { headers: form.getHeaders()}
).then (result => {
console.log (result.data)
}).catch (function() {
console.log ('Failure')
})
var express = require('express')
var app = express()
var multer = require('multer')
var storage = multer.diskStorage ({
destination : './uploads',
filename: function (req, file, cb) {
cb (null, file.originalname)
}
})
var upload = multer({ storage: storage }).single('file');
app.post('/', upload, function (req, res){
console.log(req.file)
res.send('Success!')
})
app.listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment