doumi9618 2017-01-11 13:58
浏览 30
已采纳

通过响应编写器编写html和直接在golang中提供文件有什么区别?

I am new to golang and i am trying to make a server which will fetch the list of files and create an html and write it to the response

i have created links of the file so that when it is clicked , then the file is downloaded

But this is not happening , instead the html is being downloaded instead of file

here is what i am doing

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "io"
)

func getFile(res http.ResponseWriter, req *http.Request) {

    files, _ := ioutil.ReadDir("./publicFolder/")
    res.Header().Set("Content-Type", "text/html; charset=utf-8")

    myHtml := `
           <!DOCTYPE html>
           <html>
           <head>
             <title>Home Network</title>
           </head>
           <body>
              <ul>`
    for _, f := range files {
    fmt.Println(f.Name())
    myHtml += (`
        <a href="./publicFolder/` + f.Name() + `" download="`+f.Name()+`" >` + f.Name() + `</a>`)

    }
    myHtml += (
    `
    </ul>
    </body>
    </html>
    `)  
    io.WriteString(res, myHtml)
}

func main() {
    http.HandleFunc("/getFile/", getFile)
    http.ListenAndServe(":8008", nil)
}

i have added download attribute to the html in tag But the html is being downloaded instead of file

i tried creating an html file and running it , in that case the file is being downloaded

here is the html which i created to test

<html>
    <head>
    </head>
    <body>
       <ul>
            <li><a href="./publicFolder/doc.txt" download="doc.txt">file1</a></li>
        </ul>
    </body>
</html>

EDIT

the content of doc.txt when downloading using the golang server

<html>
    <head>
    <title>
        Home Network
    </title>
    </head>
    <body>
        <ul>
        <li>
          <a href="./publicFolder/GAMES.cpp" download="GAMES.cpp">GAMES.cpp</a>
        </li>
        <li>
          <a href="./publicFolder/doc.txt" download="doc.txt">doc.txt</a>
        </li>
        <li>
          <a href="./publicFolder/erlang" download="erlang">erlang</a>
        </li>
    </ul>
    </body>
    </html>

Content of doc.txt when downloading from html file(and this is the actual content )

this is a test file doct.txt

EDIT i have added this FileServer

http.Handle("/getFile/publicFolder/", http.StripPrefix("/getFile/publicFolder/", http.FileServer(http.Dir("/publicFolder"))))
  • 写回答

2条回答 默认 最新

  • doujia1904 2017-01-11 14:26
    关注

    When you open a local HTML file in your browser, then the browser will read the HTML file locally, from your disk. You will see a URL and protocol something like file:///home/bob/test.html. In this case your Go web server is not called (it's excluded from the "loop"). Since you use relative paths in the href attributes of your <a> tags, they will also point to local files (file:/// protocol), so the browser will also read the linked files directly, "avoiding" your Go web server.

    If you serve the HTML from Go (HTTP handler as in your example), and you access it from the browser, then the browser will not read the file, it will perform another HTTP GET as your link <a> dictates. Your Go server has to read it and send it.

    Again, check out this answer how you can serve files from Go: Include js file in Go template

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大