Home > Blockchain >  Confusion between buffer file and csv when using csv-parse in nodejs
Confusion between buffer file and csv when using csv-parse in nodejs

Time:08-08

I want to parse a csv file coming from a react app. I have a controller which gets the csv file:

@Post(':id/transactions/upload')
  @UseInterceptors(FileInterceptor('file'))
  uploadTransactions(
    @Body() body: any,
    @UploadedFile() file: Express.Multer.File,
  ) {
    return this.accountsUsersService.uploadTransactions(file.buffer);
  }

and following service:

async uploadTransactions(buffer) {
    console.log('### buffer', buffer);
    const readBuffer = buffer.toString();
    console.log('### readBuffer', readBuffer);
    const csvData = [];
    createReadStream(buffer) // tried many tings
      .pipe(
        parse({
          delimiter: ';',
        })
        .on('data', function (dataRow) {
          csvData.push(dataRow);
        })
        .on('end', function() {
          console.log('### csvData', csvData)
        })
      )

    return 'uploaded transactions successfully';
  }

Here are the console.logs:

### buffer <Buffer 61 6c 62 65 72 74 3b 74 65 73 74 0a 6a 6f 61 6e 61 3b 67 6f 6f 64 0a>
### readBuffer albert;test
joana;good

Error: ENOENT: no such file or directory, open 'albert;test
joana;good
'

and here is the csv file:

albert;test
joana;good

Could someone clarify what am I missing?

CodePudding user response:

The issue that you're currently experiencing is explained here

It appears that the first argument of createReadStream must be a file path, if you pass a Buffer, it simply calls .toString() to it.

CodePudding user response:

No need to work with the Buffer, which has no path. Using the FileInterceptor provides the file path, because you can store it in the diskStorage

@Post(':id/transactions/upload')
  @UseInterceptors(
    FileInterceptor('file', {
      storage: diskStorage({
        destination: '.files',
      }),
    }),
  )
  uploadTransactions(
    @Body() body: any,
    @UploadedFile() file: Express.Multer.File,
  ) {
    console.log('### file', file.path);
    return this.accountsUsersService.uploadTransactions(file);
  }
  • Related