MrBigboka
MrBigboka

Reputation: 449

Upload problem in server "undefined" because I wanted the name of the file

Hi everyone so I just wanted to know where I could find the file name in my object. I did a console.log to find the property but I was a little bit lost. I tried to do ${data.name} but it says undefined. Anyone know where can I find it in my request ?

//the function
    async onUpload() {
      const formData = new FormData();
      formData.append('image', this.ModalFile, this.ModalFile.name);
      await axios.post('/publications/uploads', formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
          Authorization: `Bearer ${this.$store.state.token}`,
        },

//router
router.post('/uploads', upload.single('image'), async (request, response) => {
  const data = await request.files[0].buffer;
  console.log(data);
  await fs.writeFile(`./uploads/${data.name}`, data);

  return response.status(200).json({ uploaded: true });
}); ```

Upvotes: 1

Views: 391

Answers (1)

I think the property is on: request.files[0]

So, it should be: request.files[0].name

Try with a console.log( request.files[0] ) before the await buffer

file = request.FILES[0]
file.name           # Gives name
file.content_type   # Gives Content type text/html etc
file.size           # Gives file's size in byte
file.buffer         # Gives file's content as a buffer
file.read()         # Reads file

Upvotes: 1

Related Questions