douci6541 2018-09-26 11:25
浏览 151
已采纳

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

When I try to get the filename from the Content-Disposition Header the go function mime.ParseMediaType() fails with the error mime: invalid media parameter


I already found out, that it works with the normal filename format:

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

But with the UTF-8 filename (defined in rfc6266) it failes with the error given in the first paragraph:

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


Here is my code:

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


Is it possible that the stdlib does not support the UTF-8 version of Content-Disposition->Filename ?

  • 写回答

1条回答 默认 最新

  • doutou1922 2018-09-26 11:58
    关注

    The quotes must surround the header value:

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

    Run it on the playground

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

报告相同问题?