dragon87836215 2017-03-30 01:24
浏览 123
已采纳

如何设置静态文件的HTTP标头?

I use gin-gonic's r.Static("files", "./files") to serve all files in the files directory. Is there a way to set headers for these file requests so I can allow CORS?

  • 写回答

1条回答 默认 最新

  • dsgsdg206050 2017-03-30 19:52
    关注

    There is an official Gin middleware providing this functionality.

    A Good starting template (from their examples)

    func main() {
        router := gin.Default()
        // - No origin allowed by default
        // - GET,POST, PUT, HEAD methods
        // - Credentials share disabled
        // - Preflight requests cached for 12 hours
        config := cors.DefaultConfig()
        config.AllowOrigins = []string{"http://google.com"}
        config.AddAllowOrigins("http://facebook.com")
        // config.AllowOrigins == []string{"http://google.com", "http://facebook.com"}
    
        router.Use(cors.New(config))
        router.Run()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部