Last active
June 18, 2018 09:20
-
-
Save sreepurnajasti/86b95ec5c36a86def0aa0e3899b75303 to your computer and use it in GitHub Desktop.
upload single file from client to server
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Simple Multer Upload Example</title> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<body> | |
<form action="/" enctype="multipart/form-data" method="post"> | |
<input type="file" name="file-to-upload"> | |
<input type="submit" value="Upload"> | |
</form> | |
</body> | |
</html> |
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
const express = require('express'); | |
const multer = require('multer'); | |
const upload = multer({ | |
dest: 'uploads/' // this saves your file into a directory called "uploads" | |
}); | |
const app = express(); | |
app.get('/', (req, res) => { | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
// It's very crucial that the file name matches the name attribute in your html | |
app.post('/', upload.single('file-to-upload'), (req, res) => { | |
res.redirect('/'); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment