0

I have tried this way sample code. I can successfully upload the file, but before that I have to validate file type and allow only *.csv files.

Community
  • 1
  • 1
Rajesh
  • 1
  • 1

2 Answers2

0

You can use mime node module for that. https://www.npmjs.com/package/mime

Atul Agrawal
  • 1,474
  • 5
  • 22
  • 41
0

You must complement sample code as follows:

  1. Edit server/datasources.json, add to storage datasources the allowedContentTypes field, an array of mime types that you want support

    ...
     "storage": {
         "name": "storage",
         "connector": "loopback-component-storage",
         "provider": "filesystem", 
         "root": "/var/www/storage",
         "maxFileSize": "52428800",
         "allowedContentTypes":["text/csv", "application/vnd.ms-excel"]
     }
    ...
    
  2. This step is optional but is a good practice. The error can be handled in callback, also can add a remote hook in the container model file common/models/container.js

     module.exports = function (container) {
       container.afterRemoteError('upload', function (ctx, next) {
        //do any with ctx.error
      });
     }
    
Community
  • 1
  • 1