我有一个用于将文件导出到excel的功能。它检测文件准备就绪的时间,然后下载。它是这样的:
private exportData() {
this.isExporting = true;
let streams = this.series.filter(s => !!s.streamId && !s.value);
let downloadToken = new Date().getTime() + '_' + (Math.round(Math.random() * 100000000)).toString();
let url = this.service.url + '/export/';
// Create invisible iframe which will navigate to request url.
// Content-Type will be application/octet-stream, which will cause
// the browser to download instead of displaying it.
let element = document.createElement('iframe');
element.style.visibility = 'hidden';
document.body.appendChild(element);
element.src = url;
// Detecting the beginning of a download is absolutely not straight
// Tried a couple of things, but all event listeners fire when the iframe is created.
// Ended up with this approach:
// https://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download
let interval = Observable.interval(500);
let subscription = interval.subscribe((value: number) => {
if (Cookie.Get('download_token') == downloadToken) {
Cookie.Expire('download_token');
this.isExporting = false;
this.exportModal.close();
subscription.unsubscribe();
}
}, (error: any) => {
this.service.HandleUiError(error);
this.isExporting = false;
});
}
但是,当文件很大时,有时会花很长时间在后端创建该文件,并且出现504 Gateway Time-out
错误,但它没有被捕获到
this.service.HandleUiError(error);
this.isExporting = false; });
有人知道为什么会发生这种情况以及如何解决吗?
如果您在服务器上运行它,则可以将超时参数更改为更长的时间。如果您使用的是Nginx,请签出启用站点的文件和可用站点的文件,并查找超时参数。在本地主机上,您可以增加sever.js文件的超时时间。我在请求上运行长子进程并在ubuntu / etc / Nginx上浏览Nginx文件时遇到类似的问题(我认为),然后发现它在哪里强制请求超时并增加超时时间解决了我的问题。