node.js - 如何从 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 中间件具有以下配置:
// 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:\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)
我已经安装了依赖项npm i @types/express-fileupload
并将其导入from import * as fileUpload from 'express-fileupload';
,但仍然无法正常工作。config/app.ts
/node_modules/@types/express-fileupload/index
解决方案
您可以使用 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);
}
});
推荐阅读
- javascript - 具有更深路径的库的流 libdefs
- c++ - tensorflow 和 tflearn c++ API
- vba - MS Access .Movenext / .moveprevious
- sql - Rails Active Records 同时使用最大值和最小值
- mysql - MySQL 远程连接(复制)
- ldap - OpenLDAP - 将 open-ldap 定义的属性添加到自定义类
- angular-material - angular5材料无法在表格中显示任何数据
- c++ - 为什么我不能使用 std::unique_ptr 作为“模板
类”的论点? - python - Python 日志记录 - 仅适用于自己导入的模块
- arrays - AngularJS 嵌套 ng-repeat 连接来自两个 JSON 数组的 ID