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

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 三菱FX系列PLC串口指令
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助