0

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.

1 Answer 1

1

You may make use of use express-fileupload.

// src/config/app.ts

private config(): void {
  this.app.use(bodyParser.json());
  this.app.use(bodyParser.urlencoded({ extended: true }));
  // Enable files upload. Don´t forget to import the package
  this.app.use(fileUpload({
    createParentPath: true
  }));
}
async app.post('/api/submit-excel', (req: Request, res: Response) => {
  try {
    if(!req.files) {
      res.send({
        status: false,
        message: 'No file uploaded'
      });
    } else {
      const order = req.files.order;

      // Process your file
      convertExcelToJson(order); 
      
      // You can also use the mv() method to place the file in a upload directory (i.e. 'uploads')
      order.mv('./uploads/' + order.name);

      // Send response
      res.status(200).json({message: 'Post request successfull'});
    }
  } catch (err) {
    res.status(500).send(err);
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Error: Cannot find module 'express-fileupload', I'm seeing this error. I've installaed the dependency npm i @types/express-fileupload and importing it import * as fileUpload from 'express-fileupload'; in my config/app.ts from /node_modules/@types/express-fileupload/index. Did I miss anything?
Remember that npm i @types/express-fileupload only installs the types for TS but not the package itself. You must run npm i express-fileupload

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.