doulong2782 2018-11-11 22:26
浏览 68

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

Below is my main.go file to upload images. Here using this go file am building a docker image.
docker build is successful. On accessing the minikube service url, get options to upload, list and delete files. But once clicked on Upload file, get site cannot be reached.

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
", 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>
        <input type="file" name="usrfile"><br>
        <input type="submit">
        </form>
        <br>
        <br>`)

}
  • 写回答

1条回答 默认 最新

  • douxun1407 2018-11-12 07:43
    关注

    You hard coded options[0] = "</br><a href = \"http://" + ipaddress + ":80/uploadfile\">Click to upload file</a></br>" and ipaddress = "localhost". But when you running it on minikube, it not accessible by localhost. I suggest to use relative path instead.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么