如何在Golang中使用文件名正则表达式查找具有所有扩展名的文件?

huangapple go评论76阅读模式
英文:

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") // 打开要下载的文件
    defer Openfile.Close() // 函数返回后关闭文件
    fmt.Println("文件:", files)
    if err != nil {
        http.Error(w, "文件未找到。", 404) // 如果文件未找到,则返回404
        return
    }

    tempBuffer := make([]byte, 512) // 创建一个字节数组以后面读取文件
    Openfile.Read(tempBuffer) // 读取文件到字节数组
    FileContentType := http.DetectContentType(tempBuffer) // 获取文件头

    FileStat, _ := Openfile.Stat() // 获取文件信息
    FileSize := strconv.FormatInt(FileStat.Size(), 10) // 将文件大小转换为字符串

    Filename := attuid + "_" + skill

    // 设置响应头
    w.Header().Set("Content-Type", FileContentType+";"+Filename)
    w.Header().Set("Content-Length", FileSize)

    Openfile.Seek(0, 0) // 由于我们已经从文件中读取了512字节,所以将偏移量重置为0
    io.Copy(w, Openfile) // 将文件复制到客户端
}
英文:

Below code can open file with name rx80_AWS.png but I want to open file with rx80_AWS* irrespective of the extension as the file names will be unique but we upload .png .pdf and .jpeg files in thee folder

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

}

答案1

得分: 3

使用filepath.Glob

files, err := filepath.Glob("certificate/rx80_AWS*")
if err != nil {
   // 处理错误
}
for _, filename := range files {
   //...处理每个文件...
}

这里有一个在 playground 上运行的示例,通过匹配 /bin/*cat(匹配 catzcat 等)。

英文:

Use filepath.Glob.

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 by matching /bin/*cat (matching cat, zcat, etc).

huangapple
  • 本文由 发表于 2022年1月5日 03:36:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/70584144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定