gpt4 book ai didi

javascript - http.FileServer 响应错误 mime "Content-Type"

转载 作者:IT王子 更新时间:2023-10-29 01:09:36 26 4
gpt4 key购买 nike

我正在使用 http.FileServer 来提供一个 mp3 文件目录,我的模板然后在 javascript 中使用 src。但是,响应使用 Content-Type text/html 而不是 audio/mpeg。如何设置 FileServer 响应的 mime 类型,我看到了这个问题 Setting the 'charset' property on the Content-Type header in the golang HTTP FileServer , 但我仍然不确定如何覆盖 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 和像这样的 strip 前缀

http.Handle("/media/", http.StripPrefix("/media/", fs))

原因在net/http.ServeMux的文档中

Patterns name fixed, rooted paths, like "/favicon.ico", or rooted subtrees, like "/images/" (note the trailing slash). Longer patterns take precedence over shorter ones, so that if there are handlers registered for both "/images/" and "/images/thumbnails/", the latter handler will be called for paths beginning "/images/thumbnails/" and the former will receive requests for any other paths in the "/images/" subtree.

仅使用 /media,您正在为一个路径注册一个处理程序,但是如果有一个尾部的斜线,它将认为它是一个 rooted subtree 并将在该树下处理请求。

关于javascript - http.FileServer 响应错误 mime "Content-Type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39520045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com