http.文件服务器响应的mime "Content-Type"错误
http.FileServer response with wrong mime "Content-Type"
我使用http。文件服务器服务的mp3文件目录,其中我的模板然后src
在javascript。响应,然而,使用Content-Type
text/html
而不是audio/mpeg
。我如何设置FileServer响应的mime类型,我看到这个问题设置'charset'属性上的内容类型头在golang HTTP文件服务器,但我仍然不确定如何覆盖mime类型。
我的代码如下所示:
fs := http.FileServer(http.Dir(dir))
http.Handle("/media", http.StripPrefix("/media", fs))
http.HandleFunc("/", p.playlistHandler)
http.ListenAndServe(":5177", nil)
得到的错误是:
HTTP "Content-Type" of "text/html" is not supported. Load of media resource http://localhost:5177/media/sample1.mp3 failed.
不是内容类型的问题。当您请求mp3时,没有调用fs
处理程序。你需要添加一个/
到你的模式/media
和像这样的条带前缀
http.Handle("/media/", http.StripPrefix("/media/", fs))
原因在net/http的文档中。ServeMux
模式名称固定,根路径,如"/favicon.ico",或根子树,比如"/images/"(注意末尾的斜杠)。较长的模式优先超过较短的,所以如果有两个处理程序都注册了"/images/"answers"/images/thumbnails/",后一个处理程序将被调用以"/images/thumbnails/"开头的路径和前者将接收请求查看"/images/"子树中的其他路径
只有/media
,你注册一个处理程序的路径,但与尾斜杠它认为它是一个rooted subtree
,并将服务于该树下的请求。
相关文章:
- 节点,express应用程序返回text/javascript响应头(Content-Type),尽管设置了applic
- X-Content-Type-Options=nosniff解决方法
- iframe - set Content Type header
- $.ajax 响应在 content-Type:application/json 中为 JSON 中的 xml 内容生成
- ajax, setRequestHeader(), Content-Type, application/x-www-fo
- 下载基于 Content-Type JavaScript Casperjs 的链接
- Reset()和class=“”;btn大蒜自动保存"type=“;重置”;实际上并没有重新设定窗体
- 在angular js的post request中设置Content-Type
- 在设置了content-type后发送后,不能设置报头
- regex:在content-type http header中匹配javascript mime类型
- 使用ngResource(1.0.6和1.1.4)更改POST请求的Content-Type会遇到麻烦
- 在pre - flight响应中Access-Control-Allow-Headers不允许Content-Type
- 将Content-Type header设置为multipart
- http.文件服务器响应的mime "Content-Type"错误
- 以编程方式在javascript中为Content-Type设置http请求标头
- Content-Type Ajax json missing
- AngularJS:忽略Content-Type参数
- 依靠 Content-Type: text/plain 来缓解恶意 JavaScript 执行作为响应是否安全
- 什么是 响应.writeHead() 方法中 {'Content-Type:':'text/plain'} 的数据类型
- 当HTTP响应头'Content-Type'设置为'application/json'