Skip to content

Instantly share code, notes, and snippets.

@sreepurnajasti
Last active June 18, 2018 09:20
Show Gist options
  • Save sreepurnajasti/86b95ec5c36a86def0aa0e3899b75303 to your computer and use it in GitHub Desktop.
Save sreepurnajasti/86b95ec5c36a86def0aa0e3899b75303 to your computer and use it in GitHub Desktop.
upload single file from client to server
<!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>
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