如何启用从浏览器下载动态生成的文件?

发布于 2024-12-20 16:46:13 字数 401 浏览 1 评论 0原文

我有一个 Web 应用程序,其中的工作流程之一,用户可以下载动态生成的文件。输入是一个表单,其中包含生成文件所需的参数。

我目前的解决方案是让他们提交此表格并提交。在 servlet 端,我将响应标头 - 内容配置更改为附件 &还提供适当的哑剧类型。

但我认为这种方法是不够的。因为文件的生成可能需要很长时间,在这种情况下,经过一定的超时后,我会在浏览器中直接收到 500 或 503 错误。我想这对于当前的方法来说是可以预料的。

我希望我的工作流程足够灵活,以便在用户提交表单后立即告诉他们文件可能需要一些时间才能生成和生成。我们将在文件准备好后立即显示该文件的链接。我想我也可以通过电子邮件将文件或此消息发送给他们,但这并不理想。

你们能给我建议解决这个问题的方法吗?我应该更具体地提供信息吗?任何帮助表示赞赏。

I have a web application in which one of the workflows, users can download files that are dynamically generated. The input is a form which has parameters needed to generate the file.

My current solution is to let them submit this form & on the servlet side I change the response header - content disposition to be an attachment & also provide an appropriate mime-type.

But I find this approach to be inadequate. Because there are chances that the generation of file can take a very long time, in such cases after a certain timeout I directly get 500 or 503 errors in the browser. I guess this is to be expected for the current approach.

I want my workflow to be flexible enough to tell the users as soon as they submit the form that it might take time for the file to generate & that we will display the link to the file as soon as it is ready. I guess I can also email the file or this message to them, but this is not ideal.

Can you guys suggest me an approach for this problem? Should I be more specific in providing information? Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女猫性小仙女 2024-12-27 16:46:13

如果您想同步执行此操作(即让用户等待文档准备好,而不是让他们在等待时离开并做其他事情),传统的方法是将他们带到“报告加载”页面。

该页面将:

  • 1) 通知他们报告正在加载。
  • 2) 自行刷新(使用元刷新标签或 javascript)
  • 3) 刷新时,检查报告是否已准备好,并且:
    • a) 如果尚未准备好,则返回步骤 1
    • b) 如果文件准备好了,就给他们。

同步有点老派,但你的问题听起来像是你想要的方法。

异步方法包括:

  • 使用 Ajax 在文档准备好后将其链接显示在页面上。
  • 有一个单独的页面显示以前生成的文档。用户可以在闲暇时访问此页面,同时他们可以浏览网站的其余部分。这需要保留生成文档的历史记录。
  • 按照您的建议,通过电子邮件发送。

If you want to do this synchronously (i.e. make the user wait for the document to be ready rather than have them go off and do other things while waiting) a traditional approach is to bring them to a "report loading" page.

This would be a page that:

  • 1) informs them that the report is loading.
  • 2) refreshes itself (either using the meta refresh tag or javascript)
  • 3) upon refresh, checks to see if the report is ready and either:
    • a) goes back to step 1 if it isn't ready
    • b) gives them the document if it is ready.

Synchronous is kind of old-school, but your question sounded like that was the approach you wanted.

Asynchronous approaches would include:

  • Use Ajax to make a link to the document appear on the page once it is ready.
  • Have a separate page that shows previously generated documents. The use can go to this page at their leisure, and, meanwhile, they can browse the rest of the site. This requires keeping a history of generated documents.
  • As you suggested, send it via e-mail.
携君以终年 2024-12-27 16:46:13

您可以使用表单数据对服务器进行异步 Ajax 调用,而不是直接提交表单。
在服务器上创建一个临时文件,并将包含下载 URL 的链接返回给客户端。
通过 Javascript 提交答案后,您可以向用户显示提示,下载链接将在一分钟内出现。不要忘记清理临时文件!

为了提交 Ajax 调用,我建议使用 Javascript 框架。看看 JQuery:

http://api.jquery.com/category/ajax/

You can make an asynchronous Ajax Call to the server with the form data instead of submiting the form direct.
On the server you create a temp file and return a link to the client with the download URL.
After submitting the answer via Javascript you can show the user a hint, that the download link will appear in a minute. Don't forget to cleanup the temp file!

For submitting the Ajax Call I would suggest using an Javascript Framework. Have a look at JQuery:

http://api.jquery.com/category/ajax/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文