douhua1890 2017-10-23 12:56
浏览 365
已采纳

Golang http写响应而无需等待完成

I'm building an application that builds a pdf file and returns it to the client whenever it receives a request.

Since some of these pdf files might take some time to generate, I would like to periodically send some sort of status update back to client while it is running.

When it's finished building the pdf file, it should be returned to the client as well.

Something akin to:

func buildReport(writer http.ResponseWriter, request *http.Request){
    //build pdf build pdf file
    for { //for example purposes only
        writer.Write([]byte("building. Please wait."))
    }
    pdf.OutputFileAndClose("report.pdf")
    //set header to pdf so that the client knows it's a PDF
    writer.Header().Set("Content-Type", "application/pdf")
    http.ServeFile(writer, request, "report.pdf")
}

func main() {
    http.HandleFunc("/", buildReport)
    http.ListenAndServe(":8081", nil)
}

Setting the header might not work, as the writer can only have one header.

  • 写回答

3条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 如何增加获取淘宝的数据订单
      • ¥15 fluent计算后处理中如何把质量分数和摩尔分数转化为体积分数
      • ¥20 asn1c编码问题,懂asn1的人很简单
      • ¥60 在排球比赛中,排球落于界限上时,人工判断存在一定的主观因素。利用图像处理的方法来计算排球落点相对界限中心位置,判断结果。
      • ¥15 使用vs2019开发的动态链路库在win7环境下使用有问题
      • ¥30 quartus prime画电路图,要发源文件和电路图每个模块批注,源程序和仿真结果
      • ¥15 MFC滚动条如何正确使用
      • ¥15 缓冲区算法求面积,基于文献
      • ¥50 电路PCB原理图设计AD19
      • ¥15 openstack 云平台搭建 创建实例错误