Skip to content

Instantly share code, notes, and snippets.

@tetrashine
Created August 23, 2020 09:35
Show Gist options
  • Select an option

  • Save tetrashine/ef427c1e54cb1fcc0b07e918360f7b03 to your computer and use it in GitHub Desktop.

Select an option

Save tetrashine/ef427c1e54cb1fcc0b07e918360f7b03 to your computer and use it in GitHub Desktop.
multer-fileupload-3
const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
const port = 8080;
app.use('/', express.static('public'));
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, '/tmp'))
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
});
var upload = multer({ storage: storage }).single('uploaded_file')
app.use('/file-upload', (req, res, next) => {
upload(req, res, function(err) {
if (err instanceof multer.MulterError) {
// A Multer error occurred when uploading.
} else if (err) {
// An unknown error occurred when uploading.
}
// Everything went fine.
});
res.send(`File successfully uploaded`);
});
app.listen(port, () => console.log(`Running aserver listing on port ${port}...`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment