下载的 Gzip 似乎已损坏 (Chrome)

问题描述

尝试在 chrome 上下载 gzip 时遇到了一些问题。我正在使用 FileSaver Javascript 库,因为我处理的文件大小足够大。

以下代码Firefox 89.0.1 上正常工作:

function sendInfluxFileRequest(url,data_dict){
    let xhr = new XMLHttpRequest(),method = "POST";
    xhr.open(method,url,true);
    xhr.setRequestHeader("Content-Type","application/json");
    xhr.responseType = "blob";
    xhr.withCredentials = true;
    xhr.onreadystatechange = function () {
            if(xhr.status === 200) {
                // Close loader popup
                closePopup('loading-popup');
                let blob = xhr.response;
                saveAs(blob,'last_test_influx_records.gz');
            } else {
                // display error block
                closePopup('loading-popup');
                alert('Error while downloading influx file');
                console.error('Error happened while receiving response from: ',url)
                console.error('Error code: ',xhr.status)
            }
        };
    xhr.send(JSON.stringify(data_dict));
    openPopup('loading-popup');
}

但不在 Chrome 90.0.4430.212 中。问题是,虽然文件被正确保存,但似乎已损坏。我首先怀疑它可能是与请求响应头相关的错误。因此,为了丢弃后端错误,我通过检查每个浏览器中涉及的字段来检查每个浏览器中的标题是否正确。一切似乎都很好;两者的格式相同。

Firefox 中的标题

请求标头:

POST /download_last_test_influxcustom HTTP/1.1
Host: 0.0.0.0:5000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip,deflate
Content-Type: application/json
Content-Length: 54
Origin: http://0.0.0.0:5000
Connection: keep-alive
Referer: http://0.0.0.0:5000/influx_management
Cookie: *****

响应头:

HTTP/1.1 200 OK
content-encoding: gzip
Content-disposition: inline; filename=1624439510223_last_test_records.gz
Content-Type: application/gzip
Content-Length: 19432
Last-Modified: Wed,23 Jun 2021 09:11:50 GMT
Cache-Control: no-cache
Etag: "1624439510.3636444-19432-3868986588"
vary: Cookie
Date: Wed,23 Jun 2021 09:11:50 GMT
Connection: keep-alive

Chrome 中的标题

请求标头:

POST /download_last_test_influxcustom HTTP/1.1
Host: 0.0.0.0:5000
Connection: keep-alive
Content-Length: 54
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/90.0.4430.212 Safari/537.36
Content-Type: application/json
Accept: */*
Origin: http://0.0.0.0:5000
Referer: http://0.0.0.0:5000/influx_management
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.9
Cookie: **************

响应头:

HTTP/1.1 200 OK
content-encoding: gzip
Content-disposition: inline; filename=1624439767432_last_test_records.gz
Content-Type: application/gzip
Content-Length: 19432
Last-Modified: Wed,23 Jun 2021 09:16:07 GMT
Cache-Control: no-cache
Etag: "1624439767.5918925-19432-3895725292"
vary: Cookie
Date: Wed,23 Jun 2021 09:16:07 GMT
Connection: keep-alive

在丢弃标头规范错误后,我开始检查 Chrome 是否以与 XMLHttpRequest 不同的方式解释 Firefox。因此,我调试了我收到的数据:

令我惊讶的是,Firefox 中的响应数据长度与 Chrome 中的响应数据长度不同:

在 Firefox 中收到 Blob

xhr.response: Blob
size: 19432
type: "application/gzip"

在 Chrome 中收到 Blob

xhr.response: Blob
size: 2063973
type: "application/gzip"

为什么会这样?我怎样才能摆脱多余的数据?我试图查找答案未成功。有人可以帮忙吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)