import multer from 'multer';
const storage = multer.diskStorage({
destination: (_, __, cb) => {
cb(null, 'uploads');
},
filename: (_, file, cb) => {
cb(null, file.originalname);
},
});
export const upload = multer({ storage });
router.post('/img', upload.single('img'), (req: Request, res: Response) => {
console.log('eeee')
console.log(req.file); // undefined
// res.status(200).json({
// message: 'Image uploaded successfully',
// image: req?.file?.filename,
// });
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="src/style.css">
</head>
<body>
<form id="form">
<input type="file" id="img" name="img">
<button type="submit">add</button>
</form>
<script src="src/script.js"></script>
</body>
</html>
form.addEventListener('submit', e => {
e.preventDefault();
const formData = new FormData(e.target)
formData.append('img', img.files[0]);
console.log(img.files[0])
console.log(img.files)
fetch('http://localhost:3002/api/auth/img', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formData),
}).then(res => res.json()).then(res => console.log(res)).catch(err => console.log(err));
});