如何启用从浏览器下载动态生成的文件?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想同步执行此操作(即让用户等待文档准备好,而不是让他们在等待时离开并做其他事情),传统的方法是将他们带到“报告加载”页面。
该页面将:
同步有点老派,但你的问题听起来像是你想要的方法。
异步方法包括:
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:
Synchronous is kind of old-school, but your question sounded like that was the approach you wanted.
Asynchronous approaches would include:
您可以使用表单数据对服务器进行异步 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/