登录
首页 >  Golang >  Go问答

无法使用 go from minikube 上传图像文件

来源:stackoverflow

时间:2024-04-06 20:03:27 316浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《无法使用 go from minikube 上传图像文件》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

下面是我用于上传图像的 main.go 文件。 这里使用这个 go 文件构建一个 docker 镜像。

docker构建成功。 访问 minikube 服务 url 时,可以获得上传、列出和删除文件的选项。 但是一旦点击上传文件,就无法访​​问获取站点。

var baseDirectory string
var ipaddress string

func main() {
    baseDirectory = "/usr/local/go/" // provide the base directory path where the files will be kept
    ipaddress = "localhost"          // provide the ip address of the webserver
    http.HandleFunc("/", homePage)
    http.HandleFunc("/uploadfile", uploadFile)
    fs := http.FileServer(http.Dir("static/"))
    http.Handle("/static/", http.StripPrefix("/static/", fs))
    err := http.ListenAndServe(":80", nil)
    if err != nil {
        fmt.Println("Error occurred ", err)
    }
}
func homePage(w http.ResponseWriter, req *http.Request) {
    var options [1]string

    options[0] = "</br><a href = \"http://" + ipaddress + ":80/uploadfile\">Click to upload file</a></br>"
    w.Header().Set("CONTENT-TYPE", "text/html; charset=UTF-8")
    fmt.Fprintf(w, "<h1>%s</h1>, <div>%s</div>", "Home Page\n", options)
}

func uploadFile(w http.ResponseWriter, req *http.Request) {
    //var s string
    if req.Method == http.MethodPost {
        f, handler, err := req.FormFile("usrfile")
        if err != nil {
            log.Println(err)
            http.Error(w, "Error uploading file", http.StatusInternalServerError)
            return
        }
        defer f.Close()
        filename := handler.Filename
        fmt.Println(filename)
        bs, err := ioutil.ReadAll(f)
        if err != nil {
            log.Println(err)
            http.Error(w, "Error reading file", http.StatusInternalServerError)
            return
        }
        fmt.Println(bs)
        err1 := ioutil.WriteFile(baseDirectory+filename, bs, 0644)
        if err != nil {
            log.Fatal(err1)
        }
        fmt.Println("Success!")
    }

    w.Header().Set("CONTENT-TYPE", "text/html; charset=UTF-8")
    fmt.Fprintf(w, `<form action="/uploadfile" method="post" enctype="multipart/form-data">
        Upload a file<br>
        &lt;input type=&quot;file&quot; name=&quot;usrfile&quot;&gt;<br>
        &lt;input type=&quot;submit&quot;&gt;
        </form>
        <br>
        <br>`)

}

解决方案


您硬编码options [0] =“
zqbzqbzqblefbbba href = \” b“
ipaddress =“ localhost” 。但是当您在 minikube 上运行它时,localhost 无法访问它。我建议改用相对路径。

options[0] = "</br><a href = \"/uploadfile\">Click to upload file</a></br>"

终于介绍完啦!小伙伴们,这篇关于《无法使用 go from minikube 上传图像文件》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>