gpt4 book ai didi

go - Golang从飞返回文件

转载 作者:行者123 更新时间:2023-12-01 22:41:16 25 4
gpt4 key购买 nike

我有ECHO框架,该框架应应要求返回文件,并且运行良好

func IniExport(c echo.Context) error{
cfg := ini.Empty()
if section, err := cfg.NewSection("test_section"); err != nil {
return c.JSON(http.StatusInternalServerError, "Problems with generation of export file.")
}
if key, err := cfg.Section("test_section").NewKey("name", "value"); err != nil {
return c.JSON(http.StatusInternalServerError, "Problems with generation of export file.")
}
cfg.SaveTo("export.ini")
defer os.Remove("export.ini")
return c.Attachment("export.ini", "export.ini")
}
但是问题是,是否可以不创建物理文件export.ini,之后不将其删除?可能以某种方式即时返回内容?
谢谢

最佳答案

我认为您需要Send Blob

发送Blob 上下文#Blob(代码int,contentType字符串,b [] byte)可以是
用于发送具有提供的内容类型的任意数据响应,并且
状态码。

func(c echo.Context) (err error) {
data := []byte(`0306703,0035866,NO_ACTION,06/19/2006
0086003,"0005866",UPDATED,06/19/2006`)
return c.Blob(http.StatusOK, "text/csv", data)
}
您可以使用 WriteTo函数先将 cfg内容写入 io.Writer,然后可以使用这些内容代替 data(在上一个代码示例中。还要确保将内容类型更改为 text/plain)

关于go - Golang从飞返回文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63674083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com