从内容处置标题获取UTF-8文件名

时间:2018-09-26 11:25:18

标签: go mime

当我尝试从Content-Disposition标头中获取文件名时,go函数mime.ParseMediaType()失败,错误为mime: invalid media parameter


我已经发现,它可以使用正常的文件名格式:

attachment; filename="Geotag_Stamp%20(1)%20(1).jpg" <-有效

但是使用UTF-8文件名(在rfc6266中定义)会失败,并出现第一段中给出的错误:

attachment; filename*=UTF-8''"Geotag_Stamp%20(1)%20(1).jpg" <-失败


这是我的代码:

package main

import (
    "fmt"
    "mime"
)

func main() {
    d, params, err := mime.ParseMediaType(`attachment; filename="Geotag_Stamp%20(1)%20(1).jpg"`)
    if err != nil {
        fmt.Println("**Normal Filename error:", err)
    }

    fmt.Println("Normal:", d, params)
    d, params, err = mime.ParseMediaType(`attachment; filename*=UTF-8''"Geotag_Stamp%20(1)%20(1).jpg"`)
    if err != nil {
        fmt.Println("**UTF-8 Filename error:", err)
    }
    fmt.Println("UTF-8",d, params)
}

On the playground


stdlib是否可能不支持Content-Disposition-> Filename的UTF-8版本?

1 个答案:

答案 0 :(得分:4)

引号必须包含标题值:

attachment; filename*="UTF-8''Geotag_Stamp%20(1)%20(1).jpg"

Run it on the playground