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 .

这是我第一次使用Os.Open,我想知道我是否可以将它用于动态图像(在 不同的目录)还是我必须每次都输入完整路径? 例如在我的FormFile中 p>

  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)
} 
  code>  pre> 
 
 上面的函数给我一个错误,找不到这样的文件或目录,但是如果我在其中放置完整路径,例如 p> 
 
 
  os.Open(“ / Home / myfiles /  Documents / pictures / horse_riding.png“)
  code>  pre> 
 
 

然后打开图像,是否可以动态获取图像路径并将其插入os.Open中? 我确实有一个FormFile,它可以获取有关传入文件的信息,但不能获取完整路径。 p> div>

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