Skip to content

Instantly share code, notes, and snippets.

@kevinchisholm
Last active March 4, 2018 16:31
Show Gist options
  • Save kevinchisholm/58337b9cf0a5728db64ddcb9159df8e0 to your computer and use it in GitHub Desktop.
Save kevinchisholm/58337b9cf0a5728db64ddcb9159df8e0 to your computer and use it in GitHub Desktop.
Node File Uploads with Multer

Node Logo

Code Examples for my Blog Post: Node File Uploads with Multer

Setup

  • Clone this repo:
git clone [email protected]:kevinchisholm/video-code-examples.git
  • Move into the project directory:
cd /node/file-uploads-with-multer/

(for example: cd ~/Desktop/video-code-examples/node/file-uploads-with-multer/)

  • Install Dependencies:
npm install

Local Web Server Instructions

Run example # 1:

node index

Run example # 2:

node index2

Run example # 3:

node index3

Open the example web page:

http://localhost:3000/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Node File Uploads with Multer - Example</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<article>
<h1>Upload an image file</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="img" />
<button type="submit">Submit</button
</form>
</article>
</body>
</html>
var serverPort = 3000,
express = require('express'),
multer = require('multer'),
upload = multer({dest: 'uploads/'}),
app = express();
//set the static folder
app.use(express.static('static'));
//add a handler for the POST / route
app.post('/', upload.single('img'), (req, res) => {
//for demonstration purposes only:
console.log(req.file);
//send a response to the client
res.send('<h2>File upload succeeded.</h2><a href="/">Upload another file<a>');
});
//start the web server
app.listen(serverPort, () => {
console.log('server running on http://localhost:' + serverPort);
});
var serverPort = 3000,
express = require('express'),
multer = require('multer'),
upload = null,
storage = null,
app = express();
//overwrite the storage variable
storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/');
},
filename: (req, file, cb) => {
//call the callback, passing it the original file name
cb(null, file.originalname);
}
});
//overwrite the upload variable
upload = multer({storage});
//set the static folder
app.use(express.static('static'));
//add a handler for the POST / route
app.post('/', upload.single('img'), (req, res) => {
//for demonstration purposes only:
console.log(req.file);
//send a response to the client
res.send('<h2>File upload succeeded.</h2><a href="/">Upload another file<a>');
});
//start the web server
app.listen(serverPort, () => {
console.log('server running on http://localhost:' + serverPort);
});
var serverPort = 3000,
express = require('express'),
multer = require('multer'),
upload = null,
storage = null,
app = express();
//overwrite the storage variable
storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/');
},
filename: (req, file, cb) => {
//regular expressions to break apart
//the original file name and extension
var regExFileName = /([\w\d_-]*)\.?[^\\\/]*$/i,
regExFileNameExtension =/\.[0-9a-z]{1,5}$/i,
fileNameBase = file.originalname.match(regExFileName)[1],
fileNameExtension = file.originalname.match(regExFileNameExtension)[0],
//build a dynamic file name using Date.now()
fileName = fileNameBase + '_' + Date.now() + fileNameExtension;
//call the callback, passing it the dynamic file name
cb(null, fileName);
}
});
//overwrite the upload variable
upload = multer({storage});
//set the static folder
app.use(express.static('static'));
//add a handler for the POST / route
app.post('/', upload.single('img'), (req, res) => {
//for demonstration purposes only:
console.log(req.file);
//send a response to the client
res.send('<h2>File upload succeeded.</h2><a href="/">Upload another file<a>');
});
//start the web server
app.listen(serverPort, () => {
console.log('server running on http://localhost:' + serverPort);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment