Golang how can I get full file path

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

Golang how can I get full file path

问题

我一直在搜索,但找不到在Go语言中获取完整文件路径的方法。我有一个常规的HTML表单,然后我尝试在后端获取所有的信息。

  1. <form method="post" enctype="multipart/form-data" action="/uploads">
  2. <p><input type="file" name="my file" id="my file"></p>
  3. <p>
  4. <input type="submit" value="Submit">
  5. </p>
  6. </form>
  7. func upload() {
  8. f, h, err := r.FormFile("my file")
  9. if err != nil {
  10. log.Println(err)
  11. http.Error(w, "Error Uploading", http.StatusInternalServerError)
  12. return
  13. }
  14. defer f.Close()
  15. println(h.Filename)
  16. }
  17. // 这样可以得到文件名,我想要的是完整路径
  18. 我尝试过使用`filepath.Dir()`但它没有起作用
  19. <details>
  20. <summary>英文:</summary>
  21. I been searching around and can not find a way to get the full file path in Go . I have a regular html form and then I try to get all the information in the backend
  22. &lt;form method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; action=&quot;/uploads&quot;&gt;
  23. &lt;p&gt;&lt;input type=&quot;file&quot; name=&quot;my file&quot; id=&quot;my file&quot;&gt;&lt;/p&gt;
  24. &lt;p&gt;
  25. &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
  26. &lt;/p&gt;
  27. func upload() {
  28. f,h,err := r.FormFile(&quot;my file&quot;)
  29. if err != nil {
  30. log.Println(err)
  31. http.Error(w,&quot;Error Uploading&quot;,http.StatusInternalServerError)
  32. return
  33. }
  34. defer f.Close()
  35. println(h.Filename)
  36. }
  37. // This gets me the name of the file, I would like the full path of it
  38. I have tried **file path.dir()** but that does not do anything
  39. </details>
  40. # 答案1
  41. **得分**: 11
  42. 这是一个示例
  43. ```go
  44. package main
  45. import (
  46. "fmt"
  47. "path/filepath"
  48. )
  49. func main() {
  50. abs,err := filepath.Abs("./hello.go")
  51. if err == nil {
  52. fmt.Println("绝对路径:", abs)
  53. }
  54. }
英文:

here is an example:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;path/filepath&quot;
  5. )
  6. func main() {
  7. abs,err := filepath.Abs(&quot;./hello.go&quot;)
  8. if err == nil {
  9. fmt.Println(&quot;Absolute:&quot;, abs)
  10. }
  11. }

答案2

得分: 4

据我所知,你无法从代码中的f值获取文件路径。因为文件数据尚未存储在磁盘中。

如果你想将文件存储到路径中,可以按照以下方式进行操作:

  1. f, h, err := r.FormFile("myfile")
  2. if err != nil {
  3. log.Println("err:", err)
  4. http.Error(w, "Error Uploading", http.StatusInternalServerError)
  5. return
  6. }
  7. defer f.Close()
  8. fmt.Println("filename:", h.Filename)
  9. bytes, err := ioutil.ReadAll(f)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. filepath := "./aa" //设置你的文件名和文件路径
  14. err = ioutil.WriteFile("aa", bytes, 0777)
  15. if err != nil {
  16. fmt.Println(err)
  17. }

请注意,这只是将文件存储到指定路径的示例代码。你需要根据实际需求修改文件路径和文件名。

英文:

As far as I know, you cannot get the filepath form the f value in your code. Because the file data is not stored in disk yet.

And you want to store the file to a path, you can do it this way.

  1. f,h,err := r.FormFile(&quot;myfile&quot;)
  2. if err != nil{
  3. log.Println(&quot;err: &quot;,err)
  4. http.Error(w,&quot;Error Uploading&quot;,http.StatusInternalServerError)
  5. return
  6. }
  7. defer f.Close()
  8. fmt.Println(&quot;filename: &quot;,h.Filename)
  9. bytes, err := ioutil.ReadAll(f)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. filepath := &quot;./aa&quot; //set your filename and filepath
  14. err = ioutil.WriteFile(&quot;aa&quot;, bytes, 0777)
  15. if err != nil {
  16. fmt.Println(err)
  17. }

huangapple
  • 本文由 发表于 2016年12月1日 12:29:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/40902673.html
匿名

发表评论

匿名网友

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

确定