javascript - 当请求花费太长时间时,检测何时以角度触发 HTTP 错误
问题描述
我有一个用于将文件导出到 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 文件时遇到了类似的问题,并找到了它要求请求超时的位置并增加它解决了我的问题。
推荐阅读
- docker - 如何让我的 dockerized Python 应用程序在 2 个单独的端口上输出?
- c - 如果我使用 QtCreator 构建,从共享对象到主应用程序的调用会中断
- swift - 使用 @dynamic 快速替代这个 Obj-C 模式?
- x509certificate2 - X509Certificate2.import 在当前文件夹下找不到文件
- ios - 有没有办法在swift 4中迭代双变量的小数部分?
- android - 是否可以在 javadocs 中链接 README 文件?
- vba - For Each Statement 遍历一堆文本框值
- android - Kotlin:在运行时找不到具有泛型的内联函数的参数
- php - 有没有办法让表单提交两次?
- javascript - 使用 forEach 将包括嵌套对象在内的数组元素作为键添加到新对象中,但对象看起来像 [Object object]