1

I'm trying to access the buffer of a file uploaded with Postman to a simple controller in Nest. The buffer exists in the request object, but I can't access it in the fileFilter of the FileInterceptor.

Example:

@Post('file')
@UseInterceptors(FileInterceptor('file'))
async testUpload(@UploadedFile() file) {
   // defined!
   console.log(file.buffer)
}

But when I try and access the file from the interceptor the buffer isn't present

@UseInterceptors(FileInterceptor('file', {
    fileFilter: (req, file, cb) => {
      // undefined
      console.log(file.buffer)
      cb(null, true)
    }
  }))
async testUpload(@UploadedFile() file) {
   // defined!
   console.log(file.buffer)
}

Am I missing something here?

jaqarrick
  • 256
  • 2
  • 5
  • Kind of figured this out, but basically, this doesn't seem possible as the point of the FileInterceptor is to avoid loading the given file into memory. If it isn't in memory, then we can't access to buffer. I ended up not using the FileInterceptor, but my solution at the time was to test the file after it had been uploaded by requesting the first few bytes of the file and using a file validator package. – jaqarrick Nov 02 '22 at 07:39

0 Answers0