Answer the question
In order to leave comments, you need to log in
How to upload files to Node.JS server?
I implement uploading several mp3 files to the server.
What is needed?
<form method="post" id="getFileForm">
<input type="file" id="getFiles" multiple>
<button type="submit">Обработать</button>
</form>
const getFileForm = document.querySelector("#getFileForm")
getFileForm.addEventListener("submit", e => {
e.preventDefault()
const getFiles = document.getElementById('getFiles').files
fetch('http://localhost:8079/admin',{
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data'
},
body: getFiles
})
})
const app = express()
app.post('/admin', getNewFiles)
const getNewFiles = async (req, res) => {
const getFiles = req.body
try {
console.log(getFiles)
} catch (error) {
return res
.status(400)
.json({
message: error.message
})
}
return res
.status(200)
.json({
success: true
})
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question