通过Ajax POST请求下载文件时出错

时间:2019-05-13 14:20:36

标签: php jquery ajax rest download

我有两个API端点:

  1. / auth /

    方法发布

    返回身份验证令牌

  2. / report / stream / from / yyyy-mm-dd / to / yyyy-mm-dd /

    方法GET

    返回.xlsx文件

    这是端点的最后一个代码

    res.set('Content-disposition', 'attachment; filename=' + report.getFilename() );
    res.set('Content-Type', 'application/vnd.openxmlformats- 
    officedocument.spreadsheetml.sheet');
    res.send(buffer);
    

通过邮递员,我可以得到没有任何错误/问题的文件。

需求:

要将这些API集成到PHP应用程序中。

场景:

PHP应用程序具有一种表格,用于询问起始/结束日期,然后对PHP文件执行AJAX请求。

PHP AJAX脚本执行两个请求(Auth和File) PHP脚本的代码:

$ch = @curl_init();
if($ch === false){
  @http_response_code(500);
  exit;
}
@curl_setopt($ch, CURLOPT_URL,            RPT_API_URL . "/auth/" );
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
@curl_setopt($ch, CURLOPT_POST,           1 );
@curl_setopt($ch, CURLOPT_POSTFIELDS,     'username='.RPT_USERNAME.'&password='.RPT_PASSWORD);
$result = @curl_exec($ch);
$errors = @curl_error($ch);
$http_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
@curl_close($ch);

if ($http_code >= 300) {
  header('HTTP/1.1 503 Service Temporarily Unavailable');
  exit;
}

$out = json_decode($result);
$token = trim($out->token);

$ch = @curl_init();
if($ch === false){
  @http_response_code(500);
  exit;
}

@curl_setopt($ch, CURLOPT_URL,            RPT_API_URL . "/report/stream/from/2019-03-01/to/2019-03-31" );
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
@curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Authorization: Bearer ' . $token));
@curl_setopt($ch, CURLOPT_HEADER,         true);
@curl_setopt($ch, CURLINFO_HEADER_OUT,    true);

$result = @curl_exec($ch);
$errors = @curl_error($ch);
$http_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
$info = @curl_getinfo($ch);
$header_size = @curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($result, 0, $header_size);
$body = substr($result, $header_size);

@curl_close($ch);

if ($http_code >= 300) {
  header('HTTP/1.1 503 Service Temporarily Unavailable');
  exit;
}

preg_match('/filename=(.*?)\s/m', $headers, $matches);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=" . $matches[1] );
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate");
echo $body;
die();

前端JS脚本是这样的:

var dataToSend= { "from": "2019-01-14", "to": "2019-01-01" };
$.ajax({
    url: aj_url,
    type: "POST",
    data: dataToSend,
    headers: {
      Accept: 'application/octet-stream',
    }
}).done(function(res) {
    console.log(res);
    const a = document.createElement('a');
    a.style = 'display: none';
    document.body.appendChild(a);
    const blob = new Blob([res], {type: 'octet/stream'});
    const url = URL.createObjectURL(blob);
    a.href = url;
    a.download = 'test.xlsx';
    a.click();
    URL.revokeObjectURL(url);
});

问题:

当我提交表单时,正确执行了AJAX PHP脚本,但是我得到的文件似乎不正确。

当我使用Excel打开它时,出现对​​话框错误消息,打开文件但工作表数量错误。

您能帮我解决错误并找到解决方法吗?

0 个答案:

没有答案