2

I am facing the error while trying to upload a file(image) via the form on AdonisJS (I referred to the official docs @AdonisJS4.1 File Uploads)

await profilePic.move(Helpers.tmpPath('uploads'), {
    name: 'custom.jpg',
    overwrite: true
})


if (!profilePic.moved()) {
    console.log('file not moved')
}
crbast
  • 2,192
  • 1
  • 11
  • 21
Haripriya
  • 17
  • 3

1 Answers1

2

Official Docs Here

HTML

<form method="POST" action="upload" enctype="multipart/form-data">
  <input type="file" name="profile_pic" />
  <button type="submit"> Submit </button>
</form>

JS

const Helpers = use('Helpers')

Route.post('upload', async ({ request }) => {
  const profilePic = request.file('profile_pic', {
    types: ['image'],
    size: '2mb'
  })

  await profilePic.move(Helpers.tmpPath('uploads'), {
    name: 'custom-name.jpg',
    overwrite: true
  })

  if (!profilePic.moved()) {
    return profilePic.error()
  }
  return 'File moved'
})

Ferin Patel
  • 3,424
  • 2
  • 17
  • 49