如何在golang中处理来自处理程序的文件

时间:2015-07-26 14:58:44

标签: go

我想知道如何从处理程序提供文件。我正在使用gogin而我已尝试过。

func DownloadHandler(c *gin.Context) {
   c.File("./downloads/file.zip")
}

func DownloadConfigs(c *gin.Context) {
   http.ServeFile(c.Writer, c.Request, "./downloads/file.zip")
}

这两种解决方案都没有点。

我对任何解决方案都持开放态度,因为杜松子酒与标准http lib兼容,我也可以使用非杜松子酒特定的解决方案

3 个答案:

答案 0 :(得分:7)

以下是使用标准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)
    }
}

答案 1 :(得分:1)

更多使用直接使用c.FileAttachment 例如:-

func DownloadHandler(c *gin.Context) {
     c.FileAttachment("./downloads/file.zip","filename.zip")
}

答案 2 :(得分:0)

只需使用gin.Context中的此函数: https://godoc.org/github.com/gin-gonic/gin#Context.File

这样的事情:

const DOWNLOADS_PATH = "downloads/"

ginRouter.GET("/download-user-file/:filename", func (ctx *gin.Context) {
    fileName := ctx.Param("filename")
    targetPath := filepath.Join(DOWNLOADS_PATH, fileName)
    //This ckeck is for example, I not sure is it can prevent all possible filename attacks - will be much better if real filename will not come from user side. I not even tryed this code
    if !strings.HasPrefix(filepath.Clean(targetPath), DOWNLOADS_PATH) {
        ctx.String(403, "Look like you attacking me")
        return
    }
    //Seems this headers needed for some browsers (for example without this headers Chrome will download files as txt)
    ctx.Header("Content-Description", "File Transfer")
    ctx.Header("Content-Transfer-Encoding", "binary")
    ctx.Header("Content-Disposition", "attachment; filename="+fileName )
    ctx.Header("Content-Type", "application/octet-stream")
    ctx.File(targetPath)
})