如何在 golang 中根据文件名正则表达式查找具有所有扩展名的文件
How to find thee files with all extension based on file name regex in golang
下面的代码可以打开名称为 rx80_AWS.png 的文件,但我想打开名称为 rx80_AWS* 的文件,而不管扩展名是什么,因为文件名是唯一的,但我们上传 .png .pdf 和 .您文件夹中的 jpeg 文件
func DownloadCert(w http.ResponseWriter, r *http.Request) {
Openfile, err := os.Open("./certificate/rx80_AWS.png") //Open the file to be downloaded later
defer Openfile.Close() //Close after function return
fmt.Println("FIle:", files)
if err != nil {
http.Error(w, "File not found.", 404) //return 404 if file is not found
return
}
tempBuffer := make([]byte, 512) //Create a byte array to read the file later
Openfile.Read(tempBuffer) //Read the file into byte
FileContentType := http.DetectContentType(tempBuffer) //Get file header
FileStat, _ := Openfile.Stat() //Get info from file
FileSize := strconv.FormatInt(FileStat.Size(), 10) //Get file size as a string
Filename := attuid + "_" + skill
//Set the headers
w.Header().Set("Content-Type", FileContentType+";"+Filename)
w.Header().Set("Content-Length", FileSize)
Openfile.Seek(0, 0) //We read 512 bytes from the file already so we reset the offset back to 0
io.Copy(w, Openfile) //'Copy' the file to the client
}
files, err := filepath.Glob("certificate/rx80_AWS*")
if err != nil {
// handle errors
}
for _, filename in files {
//...handle each file...
}
Here is an example that works with the playground 通过匹配 /bin/*cat
(匹配 cat
、zcat
等)。
下面的代码可以打开名称为 rx80_AWS.png 的文件,但我想打开名称为 rx80_AWS* 的文件,而不管扩展名是什么,因为文件名是唯一的,但我们上传 .png .pdf 和 .您文件夹中的 jpeg 文件
func DownloadCert(w http.ResponseWriter, r *http.Request) {
Openfile, err := os.Open("./certificate/rx80_AWS.png") //Open the file to be downloaded later
defer Openfile.Close() //Close after function return
fmt.Println("FIle:", files)
if err != nil {
http.Error(w, "File not found.", 404) //return 404 if file is not found
return
}
tempBuffer := make([]byte, 512) //Create a byte array to read the file later
Openfile.Read(tempBuffer) //Read the file into byte
FileContentType := http.DetectContentType(tempBuffer) //Get file header
FileStat, _ := Openfile.Stat() //Get info from file
FileSize := strconv.FormatInt(FileStat.Size(), 10) //Get file size as a string
Filename := attuid + "_" + skill
//Set the headers
w.Header().Set("Content-Type", FileContentType+";"+Filename)
w.Header().Set("Content-Length", FileSize)
Openfile.Seek(0, 0) //We read 512 bytes from the file already so we reset the offset back to 0
io.Copy(w, Openfile) //'Copy' the file to the client
}
files, err := filepath.Glob("certificate/rx80_AWS*")
if err != nil {
// handle errors
}
for _, filename in files {
//...handle each file...
}
Here is an example that works with the playground 通过匹配 /bin/*cat
(匹配 cat
、zcat
等)。