使用php

时间:2018-10-21 15:57:08

标签: php ajax server client slim

我在使用php附加文件时遇到问题。如果我直接访问文件php,则不会发生任何问题。但是,如果我实现了我的Web服务,它将无法正常工作。 这是file.php。这不是我的php代码的全部内容,只是附加文件的一部分

$file = "..\output\yunike@gmail.com\dump_bib_id_ayt_texts.sql";
if (file_exists($file)) {
  header('Content-Description: File Transfer');
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; 
  filename="'.basename($file).'"');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: ' . filesize($file));
  ob_clean();
  flush();
  readfile($file);
  exit;
}else{
  return "file empty";
}

这是我的root.php。我正在使用苗条的框架。

$app->post('/prosesData',function (Request $request, Response $response, array $args){
  $data = $request->getParsedBody();
  $obj = new ProsesData();
  $body = $obj->proses($data['file1'],$data['file2'],$data['email']);
  $response->getBody()->write(json_encode($body));
  $newResponse = $response->withHeader('Content-type', 'application/json')->withHeader('Access-Control-Allow-Origin', '*');
  return $newResponse;
});

我使用ajax在php中调用函数

$.post('http://localhost:8800/public/prosesData', {'file1': file1, 'file2': file2, 'email': email}, function(data){
    $('#hasil tr:last').remove();
    $(".hasil").append("<tr><td>"+id+"</td><td>"+data+" </td> 
    <td><button id='checkFile1'>LANJUTKAN MEMECAH PERKATA FILE 1</button></td></tr>");
    id++;
});

结果不附加文件,但显示我要附加的文本内容。为什么会发生?谢谢

0 个答案:

没有答案