0

I using adonis.js

I try code like this :

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
const FileReader = require('file-reader');

export default class MyController {
  public async test({ request, response }: HttpContextContract) {;
    const fileToBase64 = async (file) =>
      new Promise((resolve, reject) => {
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => resolve(reader.result)
        reader.onerror = (e) => reject(e)
      })

    const file = request.file('files')
    const imageStr = await fileToBase64(file)
    return imageStr
  }
}

But if I run the function, there exist error like this :

invalid glob pattern: undefined

How can I solve this error?

The result reques.file('files') :

{
    "fieldName": "files[]",
    "clientName": "test.xlsx",
    "size": 8212,
    "type": "application",
    "extname": "xlsx",
    "subtype": "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "state": "consumed",
    "isValid": true,
    "validated": true,
    "errors": [],
    "meta": {}
}
10
  • The module you're using, file-reader, doesn't do what you think it does. For reading files in Node.js, use fs. Commented Aug 5, 2022 at 7:11
  • @robertklep I had try it. But it using path. My case is not use path but request uploaded file like above Commented Aug 5, 2022 at 7:14
  • Why aren't you using any of the provided functionality that Adonis.js offers for file uploads? Like move it to a local folder, then read it using fs? Commented Aug 5, 2022 at 7:19
  • @robertklep Yes you are right. But my case is not using local folder. The Uploaded files are sent to a file server/an external api Commented Aug 5, 2022 at 7:26
  • 1
    @robertklep Thank you very much. I had try it and it works Commented Aug 5, 2022 at 13:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.