英文:
How to open images in webserver
问题
我有一个简单的网络服务器,我想在浏览器中打开图片。问题是浏览器无法打开我发送的图片。
以下是你的代码的中文翻译:
package main
import (
"io/ioutil"
"net/http"
"io"
"html/template"
"fmt"
)
func main() {
http.HandleFunc("/images", images)
http.ListenAndServe(":8080", nil)
}
func images(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/link.html")
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
t.ExecuteTemplate(w, "link", nil)
}
这是你的 HTML 模板文件 link.html 的内容:
{{ define "link" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p><a href="/images/2.jpg">apple</a></p>
<br>
</body>
</html>
{{ end }}
我不明白为什么它不起作用。非常希望能得到帮助。此外,我想将所有文件添加到这个 Go 项目中。
英文:
I have a simple webserver and I want to open imagesin browser. The problem is browser can't open an image I sent.
package main
import (
"io/ioutil"
"net/http"
"io"
"html/template"
"fmt"
)
func main() {
http.HandleFunc("/images", images)
http.ListenAndServe(":8080", nil)
}
func images(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/link.html")
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
t.ExecuteTemplate(w, "link", nil)
}
Also my html template package, where I create a link to file on my computer. Called link.html
{{ define "link" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p> <a href="/images/2.jpg">apple</a></p>
<br>
</body>
</html>
{{ end }}
I do not understand why it doesn't work. Will be very glad for help. In addition all files I wanted to add to server are lying in this golang project
答案1
得分: 2
这是你要翻译的内容:
这是因为你没有任何专用的路由来处理任何图像的请求。
我的建议是初始化一个基于URI路径名提供文件的HTTP处理程序。你可以使用该处理程序来提供图像。
fs := http.FileServer(http.Dir("images"))
然后绑定它:
http.Handle("/images/", http.StripPrefix("/images/", fs))
这是带有我的建议的完整代码:
package main
import (
"fmt"
"html/template"
"net/http"
)
func main() {
// 这里我们创建了一个文件处理程序。
fs := http.FileServer(http.Dir("images"))
http.HandleFunc("/images", images)
// 这里我们将处理程序绑定到"/images"路由。
http.Handle("/images/", http.StripPrefix("/images/", fs))
http.ListenAndServe(":8080", nil)
}
func images(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/link.html")
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
t.ExecuteTemplate(w, "link", nil)
}
英文:
It's because you do not have any dedicated route that handles requests for any images.
My recommendation would be to initialize an HTTP handler that serves file based on the URI pathname. You can use that handler as a way to serve images.
fs := http.FileServer(http.Dir("images"))
And then to bind it:
http.Handle("/images/", http.StripPrefix("/images/", fs))
Here's your full code with my suggestions:
package main
import (
"fmt"
"html/template"
"net/http"
)
func main() {
// We're creating a file handler, here.
fs := http.FileServer(http.Dir("images"))
http.HandleFunc("/images", images)
// We're binding the handler to the `/images` route, here.
http.Handle("/images/", http.StripPrefix("/images/", fs))
http.ListenAndServe(":8080", nil)
}
func images(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/link.html")
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
t.ExecuteTemplate(w, "link", nil)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论