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

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

报告相同问题?

悬赏问题

  • ¥100 复现论文:matlab仿真代码编写
  • ¥15 esp32驱动GC9A01循环播放视频
  • ¥15 惠普360g9的最新bios
  • ¥30 这个功能用什么软件发合适?
  • ¥60 微信小程序,取消订单,偶尔订单没有改变状态
  • ¥15 用pytorch实现PPO算法
  • ¥15 关于调制信号的星座图?
  • ¥30 前端传参时,后端接收不到参数
  • ¥15 这是有什么问题吗,我检查许可证了但是显示有呢
  • ¥15 机器学习预测遇到的目标函数问题