duanqu9279 2017-01-14 05:09
浏览 116
已采纳

Golang os.Open是否可以将表单文件与动态文件一起使用

This is my first time using Os.Open and I was wondering can I use that for dynamic images (Images found in different directories) or do I have to put the full path every single time ? For instance in my FormFile

func ExampleFunc(w http.ResponseWriter, t *http.Request) {
     t.ParseForm()
     f, h, err := t.FormFile("file")
       if err != nil {
        print(err) }
       os.Open(h.Filename)
}

The function above gives me an error no such file or directory found however if I put the full path in there such as

os.Open("/Home/myfiles/Documents/pictures/horse_riding.png")

Then the image opens, is there a way of dynamically get the Image path and inserting that in os.Open ? I do have a FormFile that gets information about the incoming file but not the full path .

  • 写回答

1条回答 默认 最新

  • dopgv00024 2017-01-14 05:23
    关注

    In a file server (such as this one), you are suppose to concatenate a root_folder to the file/path you get from your form.

    filepath := path.Join((root_folder), h.Filename)
    

    That way, you open files which are within a certain root folder, instead of any file on your system (which is not secure at all).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?