I
I
Ivan Tabakerka2018-11-01 21:33:20
JavaScript
Ivan Tabakerka, 2018-11-01 21:33:20

How to upload files to Node.JS server?

I implement uploading several mp3 files to the server.
What is needed?

  1. Send files from client to server
  2. Save files to server directory

Here is some
HTML work
<form method="post" id="getFileForm">
   <input type="file" id="getFiles" multiple>
   <button type="submit">Обработать</button>
</form>

JavaScript on the client side
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
  })
})

Server Side JavaScript
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
    })
}

Thank you all in advance for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Interface, 2018-11-01
@IvanTabakerka

www.live-notes.ru/coding/node-js-stream-upload

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question