duanluo5096 2015-03-03 22:17
浏览 2391
已采纳

如何解析Content-Disposition标头以检索filename属性?

Using go, how can I parse the Content-Disposition header retrieved from an http HEAD request to obtain the filename of the file?

Additionally, how do I retrieve the header itself from the http HEAD response? Is something like this correct?

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

The mime/multipart package specifies a method on the Part type that returns the filename (called FileName), but it's not clear to me how I should construct a Part, or from what.

  • 写回答

1条回答 默认 最新

  • doumou3883 2015-03-04 01:37
    关注

    You can parse the Content-Disposition header using the mime.ParseMediaType function.

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

    This will also work for Unicode file names in the header (e.g. Content-Disposition: attachment;filename*="UTF-8''fo%c3%b6.png").

    You can experiment with this here: http://play.golang.org/p/AjWbJB8vUk

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog