Search code examples
gomime

Get UTF-8 Filename from Content-Disposition Header


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 ?


Solution

  • The quotes must surround the header value:

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

    Run it on the playground