如何解析 Content-Disposition header 以检索文件名 属性?

How can I parse the Content-Disposition header to retrieve the filename property?

如何使用 go 解析从 http HEAD 请求中检索到的 Content-Disposition header 以获取文件的文件名?

此外,如何从 http HEAD 响应中检索 header 本身?这样的说法正确吗?

resp, err := http.Head("http://example.com/")
//handle error
contentDisposition := resp.Header.Get("Content-Disposition")

mime/multipart 包在 Part 类型上指定了一个方法,returns 文件名(称为 FileName),但我不清楚我应该如何构建一个 Part,或者从什么构建一个 Part。

您可以使用 mime.ParseMediaType 函数解析 Content-Disposition header。

disposition, params, err := mime.ParseMediaType(`attachment;filename="foo.png"`)
filename := params["filename"] // set to "foo.png"

这也适用于 header 中的 Unicode 文件名(例如 Content-Disposition: attachment;filename*="UTF-8''fo%c3%b6.png")。

您可以在这里进行试验:http://play.golang.org/p/AjWbJB8vUk