I understand that due to security reasons a browser doesn't allow me to access the full path of a file when selected via FileBrowser in a file input field.
Still, I am facing a problem where this feature is needed. Maybe someone can provide an alternative solution where I don't have to reinvent any wheels.
The situation is as follows.
Questions
I don't understand why you'd want to be reusing either <input type="file">
or how you'd use the system filebrowser to be able to do this.
Your problem statement would be much easier to solve if you simply listed the files available in a directory, and then performed an action based on that information.
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func main() {
r := http.NewServeMux()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err)
}
out := "<ul>"
for _, f := range files {
v := url.Values{}
v.Add("file", f.Name())
out += fmt.Sprintf(`<li><a href="/do?%s">%s</a></li>`, v.Encode(), f.Name())
}
out += "</ul>"
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, out)
})
r.HandleFunc("/do", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, r.URL.Query().Get("file"))
})
server := &http.Server{
Addr: ":8080",
Handler: r,
}
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
https://play.golang.org/p/bSgm4nAUUYg
Note: This code is not production safe. You have to worry about people potentially accessing directories above where you are
..
, and all sorts of fun stuff that comes with sharing server information. But, it's just intended to help you get started.
Then, you don't have to worry about what the browser is passing to you, you're completely in control.