0
我有一個簡單的網絡服務器,我想在瀏覽器中打開圖像。問題是瀏覽器無法打開我發送的圖像。如何在網絡服務器中打開圖像
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 }}
我不明白爲什麼它不起作用。將非常高興的幫助。此外,我想添加到服務器的所有文件都趴在這個golang項目
謝謝你的幫助。有用! –