[求助贴] img 标签使用 src 属性无法显示 svg 图像 - V2EX {"@context": "https://schema.org", "itemListElement": [{"position": 1, "@type": "ListItem", "item": "go/fe", "name": "前端开发"}, {"position": 2, "@type": "ListItem", "item": "t/1002433", "name": "[求助贴] img 标签使用 src 属性无法显示 svg 图像"}], "@type": "BreadcrumbList"}
yyhuan

[求助贴] img 标签使用 src 属性无法显示 svg 图像

  •  
  •   yyhuan Dec 21, 2023 1975 views
    This topic created in 874 days ago, the information mentioned may be changed or developed.

    我在 vue 前端项目中,需要显示一张图片,图片 url 地址从接口获取,我无法从数据分辨图像类型(可能是 jpg/png/svg )等

    1. 当我直接使用 img 标签,尝试使用 ur 显示这个图像时,svg 格式的图像无法正常显示
    <img :src="url"> 
    1. 我直接在浏览器输入该 svg 图像的 url ,回车以后会直接下载该图片
    2. 我尝试使用 axios({ method: "get", url, responseType: "blob" }) 请求图像,可以成功获取,并通过以下方式将 svg 图像显示出来,请求如下:
    axios({ method: "get", url: item.url, responseType: "blob" }) .then((res: any) => { if (res?.hasError) { item.error = true; instance.delete(item.key); return; } let blob: any; if (item.mediaType === "IMAGE_SVG" || item.mediaType === "104" || res?.headers?.["content-type"] === "image/svg+xml") { blob = new Blob([res.data], { type: "image/svg+xml" }); } else { blob = new Blob([res.data]); } el.setAttribute("src", URL.createObjectURL(blob)); instance.caches.set(item.key, { status: 1, result: URL.createObjectURL(blob), responseData: res }); // 动态获取大小 if (res?.data?.size) { item.size = res?.data?.size; } item.error = false; }) 

    请求返回的响应信息如下:

    Cache-Control: no-cache, no-store, max-age=0, must-revalidate Cache-Control: max-age=0 Cache-Control: max-age=0 Connection: keep-alive Content-Disposition: attachment; filename="”è°.svg" Content-Type: application/x-download;charset= Date: Thu, 21 Dec 2023 14:08:30 GMT Expires: 0 Keep-Alive: timeout=4 Pragma: no-cache Proxy-Connection: keep-alive Server: nginx/1.20.1 Transfer-Encoding: chunked Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers X-Content-Type-Options: nosniff X-Xss-Protection: 1; mode=block 

    想求助下大佬们,为什么直接使用 img 标签的 src 无法显示 svg 图像?

    5 replies    2023-12-26 20:38:25 +08:00
    yyhuan
        1
    yyhuan  
    OP
       Dec 21, 2023
    额。。。难道需要设置 content-type: image/svg+xml ?
    DarknessAF
        2
    DarknessAF  
       Dec 21, 2023   1
    是这个内容类型的问题,Content-Type: application/x-download; ,这种内容类型是浏览器会直接下载的,你可以先获取好 svg 文件的数据,然后用一个元素作为占位符,把获取到的 svg 数据直接插入占位符中,但是你需要注意下 vue 的 html 转码,推荐用下 v-html 。你也可以先 log 下获取到的 svg 数据是不是正常的。
    yyhuan
        3
    yyhuan  
    OP
       Dec 22, 2023
    @DarknessAF 感谢~ 但是我还有点疑问是,我通过 img 标签的 src 显示图像时,jpeg 、svg 等类型的图像返回的响应头信息是
    Content-Disposition: attachment; filename="1_25.4k.jpeg"
    Content-Type: application/x-download;charset=
    都是 application/x-download ,但 jpeg 的图片可以显示,svg 的图像不能显示,这是因为浏览器的内容嗅探机制么?
    DarknessAF
        4
    DarknessAF  
       Dec 22, 2023
    > 这个可能是因为浏览器的没有兼容 svg 类型的数据流而已
    > 内容类型 为 Content-Type: application/x-download; 时可能浏览器会直接用 src 读取对应的数据流。
    > img 标签有还有一种用法是这样的

    ```
    <img src="data:image/svg+xml;utf8,<svg>...</svg>">
    ```

    这个网站有各种数据的转换示例,你可以看看。
    https://base64.guru/converter/encode/image/svg
    yyhuan
        5
    yyhuan  
    OP
       Dec 26, 2023
    @DarknessAF 非常感谢大佬
    About     Help     Advertise     Blog     API     FAQ     Solana     1176 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 34ms UTC 23:29 PVG 07:29 LAX 16:29JFK 19:29
    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