duanji9677 2015-02-26 09:53
浏览 26
已采纳

在jquery和PHP中创建excel之前显示消息

I am creating a web app in my company. The user can click on a button and an csv is created with MySQL data.

So far so god.

In jquery, when the user clicks the button it redirect to:

document.location.href = '/SDR/SDRJSON.php?type=' + id;

On PHP the csv file is created:

I connect to the database and create a the csv file:

while($row = $stmt->fetch(PDO::FETCH_NUM))
{
    array_push($csv, $row);
}

$fp = fopen('file.csv', 'w');

foreach ($csv as $row) {

    fputcsv($fp, $row, ';');
}

$FileName = 'PEW_'.$CountryCode;

fclose($fp);
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header("Content-Disposition: attachment; filename='".$FileName."'.csv");
header("Pragma: public");
header("Expires: 0");
echo "\xEF\xBB\xBF"; // UTF-8 BOM
readfile('file.csv');

On the page where the button is, the user clicks there and the page starts waiting for the server and then the csv file starts downloading.

For small files is ok, because it is instantaneous. But for larger files it takes like 10 / 15 seconds. Is it possible to show a message while the page waits for the server?

  • 写回答

1条回答 默认 最新

  • doufei2662 2015-02-26 10:05
    关注

    I don't Think PHP can echo while the csv is being made ... What you could do is split the "Document Formation" and "Document Download" into two parts.

    Let Ajax Make a query for the CSV to be made . And when that has been completed the PHP (Document Formation) will echo the Path of the File.

    Then After that You can use document.location.href to Newly Created File.

    I ll give the code

    ajax-code

    $('#sample-button').click(function(){
    $.ajax({
     url : '/SDR/SDRJSON.php?type=' + id,
     success : function(data){
      if(data.url)
      {
       var urlToDownload = data.url;
       alert("File is ready for download");
       document.location.href = "http://www.domain.com/file/path/"+data.url;
       // Make sure data.url has the full path or append the data.url 
       // with some strings to make sure the full path is reflected 
       // into document.location.href ... 
      }
      else
      {
        alert("Something went wrong");
      }
     }
    });
    alert("CSV is being prepared Please wait... ");
    });
    

    documentFormation.php

    while($row = $stmt->fetch(PDO::FETCH_NUM))
    {
        array_push($csv, $row);
    }
    
    $FileName = 'PEW_'.$CountryCode;
    $fp = fopen($FileName, 'w');
    
    foreach ($csv as $row) {
    
        fputcsv($fp, $row, ';');
    }
    
    fclose($fp);
    $response = array();
    $response['url'] = $FileName;
    
    echo json_encode($response); // You can handle rest of the cases where to display errors (if you have any)
    // Your DocumentFormation PHP Ends here. No need for header() or readFile.
    

    If you dont want the file to stay on server , Edit the document href to This PHP passing 'path' as the parameter

    document.location.href = "documentDownload.php?path="+data.url;

    documentDownload.php

    $path = $_GET['path'];
    $filename = end(explode("/" , $path));
    header('Content-Encoding: UTF-8');
    header('Content-type: text/csv; charset=UTF-8');
    //Assuming $filename is like 'xyz.csv'
    header("Content-Disposition: attachment; filename='".$filename);
    header("Pragma: public");
    header("Expires: 0");
    echo "\xEF\xBB\xBF"; // UTF-8 BOM
    // Reading file contents
    readfile('Relative Path to File'.$path);
    // Deleting after Read
    unlink('Relative Path to File'.$path); // To Delete right after reading
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制