我想知道如何從處理程序提供文件。我正在使用go和gin并且我已經(jīng)嘗試這樣做了。func DownloadHandler(c *gin.Context) { c.File("./downloads/file.zip")}和func DownloadConfigs(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "./downloads/file.zip")}并且這兩個(gè)解決方案也沒有點(diǎn)。我對(duì)任何解決方案持開放態(tài)度,因?yàn)?gin 與標(biāo)準(zhǔn)的 http lib 兼容,我也可以使用非 gin 特定的解決方案
3 回答

HUX布斯
TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是使用標(biāo)準(zhǔn) http 包的完整工作示例。請(qǐng)注意,您使用的文件名或路徑是相對(duì)于您當(dāng)前工作目錄的。
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "file")
})
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
- 3 回答
- 0 關(guān)注
- 163 瀏覽
添加回答
舉報(bào)
0/150
提交
取消