Skip to content

Instantly share code, notes, and snippets.

@prongbang
Created March 2, 2023 10:54
Show Gist options
  • Save prongbang/6dd132621052fdfe8ece5efdfa9bc856 to your computer and use it in GitHub Desktop.
Save prongbang/6dd132621052fdfe8ece5efdfa9bc856 to your computer and use it in GitHub Desktop.
Response XLSX data
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Post("/xlsx", func(c *fiber.Ctx) error {
fileBytes, err := ioutil.ReadFile("Excel.xlsx")
if err != nil {
panic(err)
}
c.Response().Header.Set("Content-Type", "application/octet-stream")
c.Response().Header.Set("Content-Disposition", "attachment; filename=workbook.xlsx")
c.Response().Header.Set("Content-Transfer-Encoding", "binary")
c.Response().Header.Set("Content-Length", fmt.Sprintf("%d", len(fileBytes)))
c.Response().Header.Set("Expires", "0")
c.Response().BodyWriter().Write(fileBytes)
return c.SendStatus(http.StatusOK)
})
log.Fatal(app.Listen(":3000"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment