提取API-文件下载324(ERR_EMPTY_RESPONSE)

时间:2018-08-21 11:12:01

标签: fetch-api

使用提取API下载文件时,我面临324问题。我的文件服务需要3-4分钟的长时间才能响应。

我在请求中设置了超时时间,并在以下情况下尝试过:

  1. 我在快速模拟服务器并将服务响应时间设置为1分钟,下载了文件。 (工作
  2. 我将服务器响应时间增加到2分钟,我得到了[无法加载资源:net :: ERR_EMPTY_RESPONSE](不起作用

这是我的请求代码:-

new Promise(function (resolve, reject) {
  var timeout = setTimeout(function () {
    reject(new Error('Request timed out'));
  }, FETCH_TIMEOUT);
  fetch(url, {
    method: 'GET',
    headers: new Headers({
      "Authorization": "Bearer " + getAuthToken()
    })
  })
    .then(function (response) {
      clearTimeout(timeout);
      window.location.reload();
      if(!response.ok) {
        throw Error(response.statusText);
      }
      if (response && response.status == 200) {
        const composition = response.headers.get('content-disposition');
        if (composition !== null) {
          contex.context.filename = composition.split(";")[1].split('=')[1];
        }

        return response.blob();
      }
      else reject(new Error('Response error'));

      throw new Error("Service response was not OK.");
    })
    .then(function (blob: any) {
      var url = window.URL.createObjectURL(blob);
      var a = document.createElement('a');
      a.href = url;
      a.download = contex.context.filename;
      a.click();
      resolve();
    })
    .catch(function (err: any) {
      reject(err);
    });
}).catch(function (error) {
  contex.updateState(error.toString())
});

这是快递服务器模拟: 我正在使用它并将等待时间设置为2分钟以模仿实际服务器。

app.get('/report/download', function (req, res) {
 setTimeout(function () {[enter image description here][1]
 var file = __dirname + '/api/card.json';

 var filename = path.basename(file);
 var mimetype = mime.lookup(file);

 res.setHeader('Content-disposition', 'attachment; filename=' + filename);
 res.setHeader('Content-type', mimetype);

 var filestream = fs.createReadStream(file);
 filestream.pipe(res);
 }, 120000);
});

下面是chrome:// net-internals。在这里我看到三个请求。我是新来的,我不知道发生了什么

t=  9643 [st=     0] +REQUEST_ALIVE  [dt=120312]
                      --> priority = "MEDIUM"
                      --> url = "http://localhost:8080/report/download"
t=  9643 [st=     0]    URL_REQUEST_DELEGATE  [dt=0]
t=  9643 [st=     0]   +URL_REQUEST_START_JOB  [dt=120311]
                        --> load_flags = 34624 (DO_NOT_SAVE_COOKIES | DO_NOT_SEND_AUTH_DATA | DO_NOT_SEND_COOKIES | MAYBE_USER_GESTURE | VERIFY_EV_CERT)
                        --> method = "GET"
                        --> url = "http://localhost:8080/report/download"
t=  9643 [st=     0]      URL_REQUEST_DELEGATE  [dt=0]
t=  9644 [st=     1]      HTTP_CACHE_GET_BACKEND  [dt=0]
t=  9644 [st=     1]      HTTP_CACHE_OPEN_ENTRY  [dt=0]
                          --> net_error = -2 (ERR_FAILED)
t=  9644 [st=     1]      HTTP_CACHE_CREATE_ENTRY  [dt=0]
t=  9644 [st=     1]      HTTP_CACHE_ADD_TO_ENTRY  [dt=0]
t=  9644 [st=     1]     +HTTP_STREAM_REQUEST  [dt=307]
t=  9644 [st=     1]        HTTP_STREAM_JOB_CONTROLLER_BOUND
                            --> source_dependency = 4402 (HTTP_STREAM_JOB_CONTROLLER)
t=  9951 [st=   308]        HTTP_STREAM_REQUEST_BOUND_TO_JOB
                            --> source_dependency = 4403 (HTTP_STREAM_JOB)
t=  9951 [st=   308]     -HTTP_STREAM_REQUEST
t=  9951 [st=   308]     +HTTP_TRANSACTION_SEND_REQUEST  [dt=1]
t=  9952 [st=   309]        HTTP_TRANSACTION_SEND_REQUEST_HEADERS
                            --> GET /report/download HTTP/1.1
                                Host: localhost:8080
                                Connection: keep-alive
                                User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
                                authorization: Bearer no-token-found
                                Accept: */*
                                Referer: http://localhost:8080/index-sgb3.html
                                Accept-Encoding: gzip, deflate, br
                                Accept-Language: en-US,en;q=0.9
t=  9952 [st=   309]     -HTTP_TRANSACTION_SEND_REQUEST
t=  9952 [st=   309]     +HTTP_TRANSACTION_READ_HEADERS  [dt=120002]
t=  9952 [st=   309]        HTTP_STREAM_PARSER_READ_HEADERS  [dt=120002]
                            --> net_error = -324 (ERR_EMPTY_RESPONSE)
t=129954 [st=120311]     -HTTP_TRANSACTION_READ_HEADERS
                          --> net_error = -324 (ERR_EMPTY_RESPONSE)
t=129954 [st=120311]   -URL_REQUEST_START_JOB
                        --> net_error = -324 (ERR_EMPTY_RESPONSE)
t=129954 [st=120311]    URL_REQUEST_DELEGATE  [dt=0]
t=129955 [st=120312] -REQUEST_ALIVE
                      --> net_error = -324 (ERR_EMPTY_RESPONSE)

问题: 有人可以给我建议如何解决此问题吗?我看过几篇文章,但是没有一个解决方案解决了这个问题。

0 个答案:

没有答案