Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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 使用post请求下载excel文件_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 使用post请求下载excel文件

Javascript 使用post请求下载excel文件,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个接收JSON并返回excel文件的API端点。端点直接发送文件,而不是指向文件的链接。如何使用jqueryajax下载文件 后端代码: public function postExcel() { // Parse request body $reqBody = json_decode(file_get_contents('php://input')); // On Valid Request if(isset($reqBody, $reqBody->regno,

我有一个接收JSON并返回excel文件的API端点。端点直接发送文件,而不是指向文件的链接。如何使用jqueryajax下载文件

后端代码:

public function postExcel() {

  // Parse request body
  $reqBody = json_decode(file_get_contents('php://input'));

  // On Valid Request
  if(isset($reqBody, $reqBody->regno, $reqBody->offset) && $reqBody->offset < 100 && $reqBody->offset >= 0) {
    // Create and add metadata to the workbook
    $workbook = new \PHPExcel();
    $workbook->getProperties()
             ->setCreator('AUScraper')
             ->setTitle('AU Results')
             ->setLastModifiedBy('AUScraper')
             ->setDescription('generated by AUScraper')
             ->setSubject('generated by AUScraper')
             ->setKeywords('anna university unofficial semester result API')
             ->setCategory('semester results');

    $worksheet = $workbook->getSheet(0);
    $worksheet->setTitle('results');

    // Get the results
    $results = $this->requestAU($reqBody->regno, $reqBody->offset);

    // Update worksheet



    //Output the file
    ob_clean();
    header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    header("Content-Disposition: attachment; filename=\"results.xlsx\"");
    header("Cache-Control: max-age=0");
    header("Expires: 0");

    $workbookWriter = new \PHPExcel_Writer_Excel2007($workbook);
    $workbookWriter->save("php://output");
  }

  // On Invalid Request
  header('Content-Type: application/json');
  http_response_code(400);
  return json_encode(['error' => 'invalid request']);
}

我不想事先将文件保存在服务器中,只想下载它。提前感谢。

上述问题中的问题是,我能够下载该文件,但它已损坏。我通过将xhr的
responseType
设置为
blob
来修复它

供参考的新AJAX代码:

let xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost/git_repo/AUScraper/app/public/api/excel');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'blob';

xhr.onload = function(e) {
  if (this.status == 200) {
    let link=document.createElement('a');
    link.href=window.URL.createObjectURL(this.response);
    link.download="results.xlsx";
    link.click();
  }
  else {
    Materialize.toast('Invalid data!', 2000);
  }
}

xhr.send(form_data);

对于这样一个模糊的问题“如何使用JQuery-AJAX下载文件”,您似乎已经获得了很多代码,可以通过这些代码来尝试和实现它。也许可以解释一下你当前的代码到底哪里出错了?@ADyson为这个模糊的问题感到抱歉。问题是,我可以通过ajax下载excel文件,但文件已损坏。我已经解决了这个问题,并在下面发布了解决方案。
let xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost/git_repo/AUScraper/app/public/api/excel');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'blob';

xhr.onload = function(e) {
  if (this.status == 200) {
    let link=document.createElement('a');
    link.href=window.URL.createObjectURL(this.response);
    link.download="results.xlsx";
    link.click();
  }
  else {
    Materialize.toast('Invalid data!', 2000);
  }
}

xhr.send(form_data);