Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css Golang:io.Copy(httpReponseWriter,os.File)与http.ServeFile()的比较_Css_Http_Go - Fatal编程技术网

Css Golang:io.Copy(httpReponseWriter,os.File)与http.ServeFile()的比较

Css Golang:io.Copy(httpReponseWriter,os.File)与http.ServeFile()的比较,css,http,go,Css,Http,Go,在意识到http包有一个内置的ServeFile方法之前,我实现了一个大致如下的静态处理程序: func StaticHandler(w http.ResponseWriter, r *http.Request) { filename := mux.Vars(r)["static"] // using gorilla/mux f, err := os.Open(fmt.Sprintf("%v/static/%v", webroot, filename)) if err

在意识到http包有一个内置的ServeFile方法之前,我实现了一个大致如下的静态处理程序:

func StaticHandler(w http.ResponseWriter, r *http.Request) {
    filename := mux.Vars(r)["static"] // using gorilla/mux
    f, err := os.Open(fmt.Sprintf("%v/static/%v", webroot, filename))

    if err != nil {
        http.NotFound(w, r)
        return
    }

    defer f.Close()

    io.Copy(w, f)
}
例如,通过以下方式链接我的样式表和图像:

<img href="/image.jpg" />
<link rel="stylesheet" type="text/css" href="/stylesheet.css">

这一切都很好,除了一件事:我的链接样式表没有被浏览器应用(在Chrome、Firefox、Midori中测试)。样式表可以提供(访问MYSITE/stylesheet.css显示css纯文本),图像可以正常加载到页面中,但我的页面都没有任何样式


想知道为什么吗?

简单回答:标题是错误的

Go将为html、JPG和PNG提供正确的标题,但css(和js)文件保留为“text/plain”,而不是“text/css”和“text/javascript”

显示正在调用的处理,我相信

无论如何,通过以下方式设置内容类型:

w.Header().Set("Content-Type", "text/css; charset=utf-8")

成功了。

这类响应的标题是什么?@SergioTulentsev看起来是这样的:HTTP/1.1200 OK内容类型:text/plain;字符集=utf-8日期:2013年10月30日星期三18:23:10 GMT传输编码:chunked@SergioTulentsev设置标题起作用了(嗯,很明显,我对这个网页还是很熟悉)。发布一个答案,我会接受。事实上,我真的很惊讶我的代码对图像有效。内置服务器将确定通过它的字节流的内容类型,但不会尝试猜测文本文档的性质(html除外)?