请问个前端问题? - V2EX
zhongs

请问个前端问题?

  •  
  •   zhongs Aug 8, 2023 1592 views
    This topic created in 1009 days ago, the information mentioned may be changed or developed.

    接口请求头返回得Content-Disposition信息如下,想截取 filename 数据,有没有更好的代码获取方式?

    attachment; filename=%E6%B5%B7%E5%85%B3%E8%BE%B9%E6%A3%80%E6%95%B0%E6%8D%AE%E5%87%BA%E5%85%A5%E5%A2%83%E6%95%B0%E6%8D%AE%E8%A1%A8.xlsx 

    目前获取方式

    const str = 'attachment; filename=%E6%B5%B7%E5%85%B3%E8%BE%B9%E6%A3%80%E6%95%B0%E6%8D%AE%E5%87%BA%E5%85%A5%E5%A2%83%E6%95%B0%E6%8D%AE%E8%A1%A8.xlsx' const filename = str.split('=')[1] 
    7 replies    2023-08-10 16:46:33 +08:00
    sailei
        1
    sailei  
       Aug 8, 2023
    decodeURIComponent(str)
    gkinxin
        2
    gkinxin  
       Aug 8, 2023
    ```
    let params = new URLSearchParams(document.location.search);
    let name = params.get("filename");
    ```
    gkinxin
        3
    gkinxin  
       Aug 8, 2023
    @gkinxin #2 看错问题,请忽略
    me1onsoda
        4
    me1onsoda  
       Aug 8, 2023
    很明显是 url 编码,这居然不解码
    Danswerme
        5
    Danswerme  
       Aug 8, 2023
    decodeURIComponent(str).split(";")[1].split('=')[1]
    jifengg
        6
    jifengg  
       Aug 9, 2023
    http header 有规范,以上直接截取的方式,只适合特定内容的。
    MDN: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Disposition
    如果是 js 代码,推荐使用 https://www.npmjs.com/package/content-disposition 库进行解析。
    其他语言可以按照关键词“Content-Disposition”进行搜索
    liuwb
        7
    liuwb  
       Aug 10, 2023   1
    const regex = /filename=(.*?)(?:;|$)/;
    const match = str.match(regex);
    const filename = match ? decodeURIComponent(match[1]) : null;
    About     Help     Advertise     Blog     API     FAQ     Solana     3287 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 45ms UTC 12:19 PVG 20:19 LAX 05:19 JFK 08:19
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86