Created
August 23, 2020 09:35
-
-
Save tetrashine/ef427c1e54cb1fcc0b07e918360f7b03 to your computer and use it in GitHub Desktop.
multer-fileupload-3
This file contains hidden or 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 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