I have an Angular frontend application which is sending the excel (.xlsx) file as form data in request body over to my Express endpoint.
Following is the function in my Angular service file:
uploadOrder(workOrder: File) {
const formData: FormData = new FormData();
formData.append('order', workOrder, workOrder.name);
return this.http.post(this.uploadOrderUrl, formData);
}
And below is the html form:
<form [formGroup]="orderUploadForm" (ngSubmit)="onSubmit()">
<input #fileUpload formControlName="orderUpload" type="file" (change)="handleOrderUpload($event.target.files)" accept=".xlsx">
<button *ngIf="fileToUpload !== null" [disabled]="loading || (fileToUpload === null)">
<span *ngIf="!loading">
Upload
</span>
</button>
</form>
My request payload is simply a binary with param named order. Below is payload source:
------WebKitFormBoundary6kTGShrkCmA5pcA4
Content-Disposition: form-data; name="order"; filename="sample-order.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
------WebKitFormBoundary6kTGShrkCmA5pcA4--
My Express middleware, written in typescript has below config:
// src/config/app.ts
private config(): void {
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: true }));
}
And the endpoint is written as such:
app.post('/api/submit-excel', (req: Request, res: Response) => {
console.log(req.body.order); // <-- req.body.order is returning undefined
convertExcelToJson(req.body.order);
res.status(200).json({ message: "Post request successfull" });
});
My req.body.order is returning undefined.
Am I missing anything in my app config file and that's why it's unable to get the data from request body? Any guidance or help is much appreciated, thank you.
Edit:
Tried using express-fileupload but is seeing the error below:
Error: Cannot find module 'express-fileupload'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (C:\Users\username\Software\Workspace\express-app\dist\config\app.js:5:20)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
I've installed the dependency npm i @types/express-fileupload and imported it import * as fileUpload from 'express-fileupload'; in config/app.ts from /node_modules/@types/express-fileupload/index, still it didn't work.