donglian3055
2016-12-02 22:36
浏览 221
已采纳

获取文件绝对路径时出现Golang运行时错误

I am almost done with an issue that has stomped me as I am new to Golang, I am basically trying to get the absolute path of a file inside the os.open method. I have been trying all types of things but nothing works

  func UploadProfile(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    infile, header, err := r.FormFile("upload_file")
    if err != nil {
        http.Error(w, "Error parsing uploaded file: "+err.Error(), http.StatusBadRequest)
        return
    }
    defer infile.Close()


       absolue_path := string(filepath.Abs(header.Filename))
                  // I want to get the absolute path in os.Open
    file, err :=  os.Open(absolute_path)
    }

for instance if I hard code the string in the os.Open like /Users/Documents/pictures/cats.jpg then the file uploads successfully . When i try to get the absolute path and put it inside the os.Open I get this error on runtime multiple-value filepath.Abs() in single-value context . Is there any other way that I can get the path of the file so that I can put it inside that method ?

图片转代码服务由CSDN问答提供 功能建议

当我刚接触Golang时,我几乎已经解决了一个困扰我的问题,我基本上是在尝试获取 os.open </ strong>方法中文件的绝对路径。 我一直在尝试所有类型的东西,但没有任何作用</ p>

  func UploadProfile(w http.ResponseWriter,r * http.Request){
 r.ParseForm()
 infile  ,标头,错误:= r.FormFile(“ upload_file”)
如果错误!=无{
 http.Error(w,“解析上传文件时出错:” + err.Error(),http.StatusBadRequest)
  return 
} 
延迟infile.Close()
 
 
 absolue_path:= string(filepath.Abs​​(header.Filename))
 //我想在os.Open 
文件中获取绝对路径 ,err:= os.Open(absolute_path)
} 
 </ code> </ pre> 
 
 

例如,如果我对os中的字符串进行硬编码。像 / Users / Documents / pictures / cats.jpg </ strong>,文件上传成功。 当我尝试获取绝对路径并将其放入os.Open时,在单值上下文中的运行时多值filepath.Abs​​()上出现此错误</ strong>。 还有什么其他方法可以获取文件的路径,以便可以将其放入该方法中?</ p> </ div>

2条回答 默认 最新

相关推荐 更多相似问题