How create an endpoint for downloading a PDF and sharing the generation progress with Server-sent events

huangapple go评论54阅读模式
英文:

How create an endpoint for downloading a PDF and sharing the generation progress with Server-sent events

问题

我有一个端点,允许用户下载PDF。由于PDF生成通常需要超过20秒,我想在我们处理过程中向前端发送关于PDF生成进度的信息。我以前从未使用过服务器发送事件(Server-sent events),但我在Spring 4.3中进行了简单的测试,效果很好。但现在我不知道如何将其用于PDF下载。

目前,我正在将PDF写入响应中("application/pdf"),但在这种情况下,我将无法返回"SseEmitter"对象。

您对于将进度流发送到前端并仍然能够提供PDF有任何建议吗?我应该使用WebSockets来替代SSE吗?也许使用两个不同的端点?欢迎任何建议。 How create an endpoint for downloading a PDF and sharing the generation progress with Server-sent events

英文:

I have an endpoint which allow the user to download a PDF. As the PDF generation usually takes more than 20 seconds, I would like to send to the frontend information about the PDF generation progress while we are processing it. I had never used Server-sent events, but I did a simple test in Spring 4.3) and it works fine. But now I don't know how to use it for the PDF download.

Currently I'm writing the PDF in the response ("application/pdf"), but in this case I won't be able to return the object SseEmitter.

Do you have any suggestion for sending a progress stream to frontend and still being able to provide the PDF? Should I use websockets instead of SSE? Maybe two different endpoints? Any suggestion is welcome. How create an endpoint for downloading a PDF and sharing the generation progress with Server-sent events

答案1

得分: 0

我的解决方案是使用了两个不同的端点:

第一个端点:提供进度流并将 PDF 内容持久化到数据库中。
第二个端点:下载 PDF。

我认为如果我之前没有在数据库中持久化这个 PDF 内容,我可能不会对这个解决方案感到满意,但事实恰恰相反,所以这样做是可以的。 How create an endpoint for downloading a PDF and sharing the generation progress with Server-sent events

英文:

My solution was using two different endpoints:

First endpoint: provide the progress stream and persist the PDF content in database
Second endpoint: download the PDF

I don't think I would be happy with this solution if I was not already persisting this PDF content in database anyway, but it was exactly the case, so it was fine. How create an endpoint for downloading a PDF and sharing the generation progress with Server-sent events

huangapple
  • 本文由 发表于 2020年9月24日 20:38:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64046619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定