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

HUX布斯
TA貢獻1876條經驗 獲得超6個贊
這是使用標準 http 包的完整工作示例。請注意,您使用的文件名或路徑是相對于您當前工作目錄的。
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 關注
- 168 瀏覽
添加回答
舉報
0/150
提交
取消