EGG Error: nodejs.Error: Invalid filename [How to Solve]

preface:

This error occurs when using the upload function of egg.

1. Error message:

Error in file name

2. Error reporting reason:

There is no configuration file information in config.default.js, but it is directly obtained in the controller, and an error CTX. Request. Files = = undefined is reported

const file = ctx.request.files[0]; //Get uploaded files

3. Solution:

Add in config.default.js:

//Enable file mode
  config.multipart = {
    mode: 'file'
  }

4. Cause new problems:   Invalid filename: order information.xlsx

5. Solutions to new problems and types have to be set. There is a parameter in the upload that is a white list

Add: whitelist: [‘. Xlsx’] in config.default.js

//Enable file mode
  config.multipart = {
    mode: 'file',
    fileSize: 1048576000,
    whitelist: ['.xlsx']
  }

That’s it!

Read More: