【问题标题】:Display message before creating excel in jquery and PHP在 jquery 和 PHP 中创建 excel 之前显示消息
【发布时间】:2015-04-28 15:16:10
【问题描述】:

我正在我的公司创建一个网络应用程序。用户可以单击一个按钮,然后使用 MySQL 数据创建一个 csv。

到目前为止,上帝。

在 jquery 中,当用户单击按钮时,它会重定向到:

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

在 PHP 上创建 csv 文件:

我连接到数据库并创建一个 csv 文件:

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');

在按钮所在的页面,用户点击,页面开始等待服务器,然后开始下载csv文件。

对于小文件是可以的,因为它是瞬时的。但对于较大的文件,它需要 10 / 15 秒。页面等待服务器时是否可以显示消息?

【问题讨论】:

    标签: php jquery excel csv


    【解决方案1】:

    我认为 PHP 在制作 csv 时无法回显...您可以做的是将“文档形成”和“文档下载”分成两部分。

    让 Ajax 查询要生成的 CSV。完成后,PHP(文档格式)将回显文件的路径。

    然后你可以使用 document.location.href 来新建文件。

    我会给出代码

    ajax 代码

    $('#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.
    

    如果您不希望文件保留在服务器上,请将文档 href 编辑为 This PHP passing 'path' as 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
    

    【讨论】:

    • 感谢您的建议。但我收到“出了点问题”警报。如果我提醒数据而不是显示 json。
    • 可能是 php 错误 .. 我没有初始化 $response .. 立即尝试 ... 您还可以使用 Any Web Developers Console 中的“NETWORK”选项卡检查 Ajax 响应浏览器
    • 您能否更具体地了解存储了哪些“数据”?尝试在此处粘贴错误.. 如果我有“数据”的内容,我可以更好地调试
    • 我收到的警报错误是 csv 文件中的每个单元格,例如 test;test;test;test;test;test;test
    • 您是否在 PHP 中回显 CSV? ...只需确保您 Echo 的唯一内容是文件路径,仅此而已。此外,您不必设置任何标题或任何此类内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多