如何从Express API POST请求主体中检索excel.xlsx数据



我有一个Angular前端应用程序,它将excel(.xlsx(文件作为请求正文中的表单数据发送到我的Express端点。

以下是我的Angular服务文件中的函数:

uploadOrder(workOrder: File) {
const formData: FormData = new FormData();
formData.append('order', workOrder, workOrder.name);
return this.http.post(this.uploadOrderUrl, formData);
}

下面是html表单:

<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>

我的请求负载只是一个名为order的二进制参数。以下是有效载荷来源:

------WebKitFormBoundary6kTGShrkCmA5pcA4
Content-Disposition: form-data; name="order"; filename="sample-order.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

------WebKitFormBoundary6kTGShrkCmA5pcA4--

我的Express中间件,用typescript编写,配置如下:

// src/config/app.ts
private config(): void {
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: true }));
}

端点是这样写的:

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" });
});

我的req.body.order正在返回undefined

我的应用程序配置文件中是否缺少任何内容,这就是为什么它无法从请求主体获取数据的原因?非常感谢任何指导或帮助,谢谢。

编辑:

尝试使用express-fileupload,但看到以下错误:

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:UsersusernameSoftwareWorkspaceexpress-appdistconfigapp.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)

我已经安装了依赖项npm i @types/express-fileupload,并将其从/node_modules/@types/express-fileupload/index导入到config/app.ts中的import * as fileUpload from 'express-fileupload';,但它仍然不起作用。

您可以使用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);
}
});

相关内容

  • 没有找到相关文章

最新更新