简体   繁体   English

导出CSV和Javascript并显示进度条

[英]Export CSV with Javascript and display progress bar

Is there a way to display a progress bar while a CSV file is being generated and served up via ajax?有没有办法在生成 CSV 文件并通过 ajax 提供时显示进度条? The CSV file takes a while to generate due to the database, and need a loader on the screen and hide it when it is done.由于数据库的原因,CSV 文件需要一段时间才能生成,并且需要在屏幕上加载一个加载程序并在完成后将其隐藏。 I would like this to be ajax or stay on the same page if necessary.我希望这是 ajax 或在必要时留在同一页面上。 Right now I am doing this below, but I can't tell when the file is done downloading to stop the progress.现在我正在下面执行此操作,但我无法确定文件何时完成下载以停止进度。

var iframe = document.createElement("iframe");
                iframe.src = (value.url);
                iframe.style.display = "none";
                document.body.appendChild(iframe);

If I understand your situation correctly you are generating a file on the server side when you receive a request, saving it to disk and then serving it up from there.如果我正确理解您的情况,您将在收到请求时在服务器端生成一个文件,将其保存到磁盘,然后从那里提供。 If that is what you are doing you have two options:如果那是您正在做的事情,您有两种选择:

  1. Update some shared state (memcached, redis, mongodb, a sqlite database or a file) from your generating code when you start generating the file with the total number of rows you have to generate and the number of rows you have currently generated.当您开始生成具有您必须生成的总行数和您当前生成的行数的文件时,从您的生成代码更新一些共享的 state(memcached、redis、mongodb、sqlite 数据库或文件)。 Make additional ajax requests back to an endpoint that will give you the current values from that shared state.向端点发出额外的 ajax 请求,该端点将为您提供共享 state 的当前值。
  2. Stream the generated CSV file straight back to the client - unless the fetching of the data takes the majority of the time this will result in a faster appearance of the download dialog and an apparent speed up. Stream 生成的 CSV 文件直接返回客户端 - 除非获取数据花费大部分时间,否则这将导致下载对话框更快出现并明显加快速度。

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

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