$ npm install express-fileupload
$ npm install @types/express-fileupload
PictureController.use(fileUpload());
PictureController.post('/PictureUpload', function(req, res) {
let uploadPath;
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('No files were uploaded.');
}
let uploadedFile: fileUpload.UploadedFile;
if (req.files.sampleFile instanceof Array) {
uploadedFile = req.files.sampleFile[0];
}
else {
uploadedFile = req.files.sampleFile;
uploadPath = __dirname + '/' + uploadedFile.name;
uploadedFile.mv(uploadPath, function(err) {
res.send('File uploaded!');
});
}
});
export default PictureController;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form ref='uploadForm'
id='uploadForm'
action='http://localhost:3000/PictureUpload'
method='post'
encType="multipart/form-data">
<input type="file" name="sampleFile" />
<input type='submit' value='Upload!' />
</form>`
</body>
</html>