Consume content of the file without saving the file with express.js?
This is what can be done when you don't want to store the csv in a file system and read the content. In my case, I had to pass the content of csv file as a string to another server (without saving in local system of server).
const multer = require('multer');const upload = multer({ storage: multer.memoryStorage() })app.post( '/users/bulkUpload', upload.single('csvFile'), this.usersRoutes.uploadUserData );
and in uploadUserData()
:
uploadUserData = async( req: Request, res: Response, next: any): Promise<any> => { try { const options = { formData : { 'upload': String(req.file.buffer) }, headers: { authorization: req.token, 'Content-type': 'multipart/form-data' }, json: true }; const response = await this.http.post.exec( '/somePostUrl/', options ); return res.status(200).json(response.body); }catch (error) { return next(error); }}
Here, console.log(String(req.file.buffer))
would show you the content of csv file as a string.
I hope it helps you.