Created
November 19, 2019 07:20
-
-
Save HarshithaKP/ebc0e79800e5638fe827c157360378be to your computer and use it in GitHub Desktop.
Multer file upload with Axios client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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