douxing6434 2019-01-17 11:15
浏览 560

Chrome文件浏览器-访问完整路径

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.

  • The backend and the operating user have both access to the same filesystem.
  • The user has to pick one or multiple file location/s and inform the backend.
  • The backend will then schedule a task.
  • Meanwhile the file contents may be altered by the user but the location will remain the same.
  • The users browser is under our control. So we could use extensions if needed.

Questions

  • Is there any Chrome option that would circumvent the security barrier and allow me to access the full path?
  • Is there any helpful Chrome extension out there?
  • Is there any known alternative solution or "best practice" advise to this problem?
  • 写回答

1条回答 默认 最新

  • dongluobei9359 2019-01-17 13:37
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名