Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么要下载excel文件;“腐败”;_Javascript_Php_Ms Office - Fatal编程技术网

Javascript 为什么要下载excel文件;“腐败”;

Javascript 为什么要下载excel文件;“腐败”;,javascript,php,ms-office,Javascript,Php,Ms Office,我正在开发一个web应用程序,它可以下载由api生成的excel报告。在postman中运行调用将返回一个打开时没有问题的文件。通过web应用程序下载该文件。该文件已损坏,Excel无法修复。如果我在记事本++中打开无法修复的损坏文件并保存它而不做任何更改,它现在已损坏,但可以通过Excel修复。此外,根据十六进制编辑器,所有三个文件都是相同的 编辑:我没有访问生成excel的API的权限,因此无法提供该代码。出于调试目的,我在服务器上使用了一个文件,同样的问题也在发生,所以它似乎在我这边的某个

我正在开发一个web应用程序,它可以下载由api生成的excel报告。在postman中运行调用将返回一个打开时没有问题的文件。通过web应用程序下载该文件。该文件已损坏,Excel无法修复。如果我在记事本++中打开无法修复的损坏文件并保存它而不做任何更改,它现在已损坏,但可以通过Excel修复。此外,根据十六进制编辑器,所有三个文件都是相同的

编辑:我没有访问生成excel的API的权限,因此无法提供该代码。出于调试目的,我在服务器上使用了一个文件,同样的问题也在发生,所以它似乎在我这边的某个地方。 中间api函数是

function exportDebuggy($request, $fileSource)
{   
    $resData = array(
        "success" => false,
        "response" => array(),
        "message" => ""
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $fileSource);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_ENCODING , "");
    curl_setopt($ch, CURLOPT_HEADER, false);
    $data = curl_exec($ch);
    $err = curl_error($ch);

    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    if ($err)
    {
        $resData['message'] = "cURL Error #:" . $err;
    }
    else if( $code === 401 )
    {
        $resData['message'] = "Client session has expired. Please re login.";
    }
    else
    {
        $resData = "";
    }

    return $resData;
}
进行调用并处理保存的js代码是

function exportDebuggy()
{
        var xmlhttp = new XMLHttpRequest();
        var endpoint = "/wp-json/data-tracker/exportDebug";

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == XMLHttpRequest.DONE) {
                if (xmlhttp.status == 200)
                {
                    var data = window.URL.createObjectURL(xmlhttp.response);
                    var link = document.createElement('a');
                    link.href = data;
                    link.download="transactionsDownloaded.xlsx";
                    link.click();
                }
                else if (xmlhttp.status == 400)
                {
                    alert('There was an error 400');
                }
                else
                {
                    alert('something else other than 200 was returned');
                }
            }
        };

        xmlhttp.open("GET", endpoint, true);
        xmlhttp.setRequestHeader( 'X-WP-Nonce', userID );
        xmlhttp.responseType = "blob";
        xmlhttp.send();
}

你能给我们看一下生成Excel的API的代码吗?“最重要的是,根据十六进制编辑器,所有三个文件都是相同的”。。。这是不可能的,至少检查一下大小,它们的字节数是否相同?“这是不可能的”这也是我的想法,但它们是一样的,文件大小也是一样的。这就是为什么我如此困惑。