如何在Web服务器中打开图像

huangapple go评论74阅读模式
英文:

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 (
 
  &quot;io/ioutil&quot;
  &quot;net/http&quot;
  &quot;io&quot;
  &quot;html/template&quot;
  &quot;fmt&quot;

 )  
func main() {



http.HandleFunc(&quot;/images&quot;, images)


http.ListenAndServe(&quot;:8080&quot;, nil)
}
func images(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles(&quot;templates/link.html&quot;)
if err != nil {
	fmt.Fprintf(w, err.Error())
	return
}

t.ExecuteTemplate(w, &quot;link&quot;, nil)
}

Also my html template package, where I create a link to file on my computer. Called link.html

  {{ define &quot;link&quot; }}


  &lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
   &lt;head&gt;
   &lt;meta charset=&quot;UTF-8&quot;&gt;
   &lt;title&gt;Title&lt;/title&gt;
   &lt;/head&gt;
   &lt;body&gt;

   &lt;p&gt; &lt;a href=&quot;/images/2.jpg&quot;&gt;apple&lt;/a&gt;&lt;/p&gt;
   &lt;br&gt;

   &lt;/body&gt;
   &lt;/html&gt;


   {{ 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(&quot;images&quot;))

And then to bind it:

http.Handle(&quot;/images/&quot;, http.StripPrefix(&quot;/images/&quot;, fs))

Here's your full code with my suggestions:

package main

import (
  &quot;fmt&quot;
  &quot;html/template&quot;
  &quot;net/http&quot;
)

func main() {
  // We&#39;re creating a file handler, here.
  fs := http.FileServer(http.Dir(&quot;images&quot;))

  http.HandleFunc(&quot;/images&quot;, images)

  // We&#39;re binding the handler to the `/images` route, here.
  http.Handle(&quot;/images/&quot;, http.StripPrefix(&quot;/images/&quot;, fs))

  http.ListenAndServe(&quot;:8080&quot;, nil)
}

func images(w http.ResponseWriter, r *http.Request) {
  t, err := template.ParseFiles(&quot;templates/link.html&quot;)
  if err != nil {
    fmt.Fprintf(w, err.Error())
    return
  }

  t.ExecuteTemplate(w, &quot;link&quot;, nil)
}

huangapple
  • 本文由 发表于 2017年7月24日 14:12:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/45273724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定