首页 > 解决方案 > 当请求花费太长时间时,检测何时以角度触发 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; });

有谁知道为什么会发生这种情况以及如何解决?

标签: javascriptangulartypescript

解决方案


如果您在服务器上运行它,您可以将超时参数更改为更长。如果您使用的是 Nginx,请检查您的站点启用和站点可用文件并查找超时参数。在本地主机上,您可以增加 sever.js 文件的超时时间。我在请求上运行长子进程并在 ubuntu /etc/Nginx (我认为)上浏览 nginx 文件时遇到了类似的问题,并找到了它要求请求超时的位置并增加它解决了我的问题。


推荐阅读