[英]JSP: save a file on client without storing it on server
In my jsp someone wants to export a query result to csv. 在我的jsp中,有人想将查询结果导出到csv。 Can't use frameworks.
不能使用框架。 At the moment, here is the process:
目前,这是一个过程:
Thing is, I' don't want to create this file on server, as I have to dispose it afterwards, but I still want to give the user the "save as" window. 事实是,我不想在服务器上创建这个文件,因为我必须在之后处理它,但我仍然想给用户“另存为”窗口。 So, is there a way, putting for example an OutputStream object in session, to achieve this result?
那么,是否有一种方法,例如在会话中放置一个OutputStream对象来实现这个结果? Thank you all!
谢谢你们!
A servlet can generate any type of content. servlet可以生成任何类型的内容。 So, when you click the button to run the servlet, simply have the servlet write the file back to the client at that time.
因此,当您单击按钮以运行servlet时,只需让servlet将文件写回客户端。 You'll need to set the
Content-type
header to " text/csv " (and making sure that you set encoding properly). 您需要将
Content-type
标头设置为“ text / csv ”(并确保正确设置编码)。 You don't need to set the Content-Length
header; 您不需要设置
Content-Length
标头; the browser can deal with that. 浏览器可以处理。
When the servlet returns data to the browser, the user will be prompted to save the file or open it with an application. 当servlet将数据返回给浏览器时,将提示用户保存文件或使用应用程序打开它。
Yes. 是。 You don't need to save the file.
您无需保存文件。 Just hold it in the session and send it in-memory to the
OutputStream
. 只需在会话中保留它并将其在内存中发送到
OutputStream
。 I would trash it after a given time or after delivery in order to save memory. 我会在给定时间后或分娩后将其丢弃以节省内存。
Set the content type and the content disposition appropriately. 适当地设置内容类型和内容配置。 This will mean that the browser interprets the output correctly and prompts your user to save it or launch the appropriate application.
这意味着浏览器会正确解释输出并提示用户保存输出或启动相应的应用程序。
See this SO answer for more details. 有关详细信息,请参阅此SO答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.