简体   繁体   English

如何导出,然后在后台在PHP中使用Ajax调用下载Excel文件

[英]how to export and then download a excel file with ajax call in background in php

I have to export large file in excel format and download it. 我必须以excel格式导出大文件并下载。 i am using php excel. 我使用PHP的出类拔萃。 File is very large so it is taking much time in export . 文件很大,因此导出需要花费很多时间。 so i am using async ajax call and on server side, i am using session_write_close. 所以我用异步Ajax调用并在服务器端,我使用session_write_close。 Through this i can send concurrent calls.this is working fine if i stay on the same page... but when i send ajax call to export and download file and after that i redirect to a new page before the completion of that ajax call then that ajax call got cancelled and unable to download file. 通过这个我可以发送并发的调用。如果我停留在同一页面上,这可以很好地工作...但是当我发送ajax调用来导出和下载文件,然后在完成该ajax调用之前我重定向到新页面然后该ajax调用被取消,无法下载文件。 how can i export a file and then download it even after user has been redirected to a new URL. 我怎样才能导出文件,然后下载用户已经被重定向到一个新的URL后还是一样。

There are several ways to perform download action on same page. 有几种方法可以在同一页面上执行下载操作。 Use of async may cause issues, your application may feel lagging between ajax operation. 使用async可能会导致问题,您的应用程序可能会在ajax操作之间感到落后。

As per my opinion you can do your ajax request in this way. 根据我的意见,您可以通过这种方式进行ajax请求。

Upon creation of file use command-line approach if its possible. 创建文件后,请尽可能使用命令行方法。 Or you can simply use system command with curl or elinks command and pass the url which is being used to generate the excel file. 或者,您可以简单地将系统命令与curl或elinks命令一起使用,并传递用于生成excel文件的url。

curl http://mysite/generate/excel?id=123

or 要么

elinks http://mysite/generate/excel?id=123

By using ajax ( sync ) you can make a request with keep alive option and check inside your code if file is created with some interval. 通过使用ajax(sync),您可以使用keep alive选项发出请求,并检查代码内部是否以一定间隔创建文件。 your code will be like this 您的代码将像这样

while (!file_exists("path/to/excel_file.xlsx")) {

   usleep(2000);
}

echo json_encode(["status" => true, "file_url" => "http://mysite/url/to/excel_file.xlsx"]);

Once you get response from ajax, On success function call below function with file url to download file through iframe on the same page. 一旦您收到来自Ajax的响应,则在成功函数上,使用文件url调用以下函数以通过iframe在同一页面上下载文件。

downloadAttachment(response.file_url)

/* Amazing download management with error handling */

var downloadAttachment = function(link){

    var iframe = document.createElement("iframe"), iframeWindow;

    iframe.src = link;

    document.body.appendChild(iframe);

    iframe.onload = function(){

        response = JSON.parse($(iframe).contents().text())

        if(!response.status)
        {
            alert(response.message);
            $(iframe).remove();
        }
    }

    setTimeout(function(){

        $(iframe).remove();

    },10000)
}

This is logical flow of how to download big files if it is taking long time to generate. 如果生成时间较长,这是如何下载大文件的逻辑流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM