2 回答
TA貢獻1826條經(jīng)驗 獲得超6個贊
http.Dir(uploadPath)是從string到http.Dir的顯式類型轉(zhuǎn)換,它只是一個帶有Open方法的字符串。
這意味著不對字符串進行任何處理,您可以filepath.Join直接對原始字符串進行處理:
filePath := filepath.Join(uploadPath, fileName)
注意:您用于http.Dir將參數(shù)轉(zhuǎn)換為的原因http.FileServer是Dir.Open實現(xiàn)http.Filesystem接口的方法。
TA貢獻1876條經(jīng)驗 獲得超7個贊
因為我在 dockerfile 中配置了我的根路徑,所以有兩種方法可以修復它。
// 1. hardcode the '/rootPath' same as the dockerfile configure.
filePath := filepath.Join("/rootPath", uploadPath, fileName)
// 2. Dynamically get the current root path.
ex, err := os.Executable()
if err != nil {
...
}
rootPath := filepath.Dir(ex)
filePath := filepath.Join(rootPath, uploadPath, fileName)
感謝 Marc 的提示。
- 2 回答
- 0 關(guān)注
- 354 瀏覽
添加回答
舉報
