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

huangapple go评论95阅读模式
英文:

Golang runtime error when getting file absolute path

问题

我几乎解决了一个问题,这个问题困扰着我,因为我对Golang还不熟悉。我基本上是想在os.open方法中获取文件的绝对路径。我尝试了各种方法,但都没有成功。

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()

    absolute_path := string(filepath.Abs(header.Filename))
    // 我想在os.Open中获取绝对路径
    file, err := os.Open(absolute_path)
}

例如,如果我在os.Open中硬编码字符串,如**/Users/Documents/pictures/cats.jpg**,那么文件上传成功。但是,当我尝试获取绝对路径并将其放入os.Open中时,运行时会出现错误multiple-value filepath.Abs() in single-value context。是否有其他方法可以获取文件的路径,以便我可以将其放入该方法中?

英文:

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 ?

答案1

得分: 2

根据文档的说明,Abs函数将返回两个值,一个是字符串,一个是错误。

因此,你不能这样写:

absolute_path := string(filepath.Abs(header.Filename))

而应该这样写:

absolute_path, err := filepath.Abs(header.Filename)

另外请注意,absolute_path已经是一个字符串。

英文:

According to documentation, Abs function will return TWO values, one string and one error.

So you can not have something like:

absolute_path := string(filepath.Abs(header.Filename))

instead, you should write:

absolute_path, err := filepath.Abs(header.Filename)

Also note that absolute_path is a string already.

答案2

得分: 2

这行代码的编译错误(multiple-value filepath.Abs() in single-value context)告诉你filepath.Abs返回了多个参数,而string只接受一个参数。

filepath.Abs返回一个string和一个error

正确的代码应该是:

ap, err := filepath.Abs(header.Filename)
if err != nil {
    // 处理错误
}
英文:

The compiler error from this line of code (multiple-value filepath.Abs() in single-value context)

 absolue_path := string(filepath.Abs(header.Filename))

is telling you that filepath.Abs is returning multiple arguments and string only takes one argument.

filepath.Abs returns a string and error

Code should be:

ap, err := filepath.Abs(header.Filename)
if err != nil {
    // handle error
}

huangapple
  • 本文由 发表于 2016年12月3日 06:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/40941918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定