作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有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/
我是一名优秀的程序员,十分优秀!