【问题标题】:Export inline svg-object (IE issue)导出内联 svg 对象(IE 问题)
【发布时间】:2014-07-12 00:14:42
【问题描述】:

核心问题:

我正在尝试使用 JavaScript 获取 SVG-Inline-Object 并将其发送到输出代表给定 SVG-Object 的服务器。

在 Chrome 和 FF 中一切正常,但 IE (>= 9) 将 SVG-Object 的给定字符串剪切到任何地方,因此发送到服务器的字符串不完整,最终导致错误。

我目前的流程:

html

<html>
<head></head>
    <body>
        <div>
            <svg>(expect a correctly generated svg-object here - works fine)</svg>
            <a href='#export'>export</a>
            <!-- click on this link triggers the javascript-function !-->
        </div>
    </body>
</html>

javascript(通过点击“导出”链接触发)

var $svg = $(this).parent().find("svg"); // works fine
var s = new XMLSerializer();
var svgContent = s.serializeToString($svg[0]);
$.post("/Results/saveSVG/", // saveSVG => store SVG-Data in Session
    { "svg" : svgContent }).done(function(data) {
    window.open("/Results/getSVG/svg.php");
});

/Results/saveSVG/(一个 PHP 文件)

$_SESSION["svg"] = $_POST["svg"]; // similar to something like this (works fine)

svg.php(在 Chrome / FF 中像魅力一样工作

header("Content-type: image/svg+xml");
echo $_SESSION["svg"];
exit;

在 Chrome / FF 中会发生什么?

  1. svgContent-变量: &lt;svg&gt;...&lt;/svg&gt;
  2. 服务器接收到的svgContent-variable (/Results/saveSVG/)
  3. /Results/getSVG/svg.php 上的输出是完美的 SVG 文件

在 IE 中会发生什么?

  1. svgContent-variable 在某处被裁剪: &lt;svg&gt;..
  2. 服务器收到无效的 svgContent-variable (/Results/saveSVG/)
  3. /Results/getSVG/svg.php 上的输出未呈现

IE 中的调试信息

  1. svgContent-variable: (Javascript)

&lt;svg xmlns="http://www.w3.org/2000/svg" width="263" height="300" xmlns="http://www.w3.org/2000/svg" xmlns:NS1="" NS1:xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"&gt;&lt;defs&gt;&lt;clipPath id="DevExpress_28"&gt;&lt;rect fill="none" stroke="none" stroke-width="0" x="0" y="0" width="263" height="300" rx="0" ry="0" /&gt;&lt;/clipPath&gt;&lt;pattern id="DevExpressPattern_46" patternUnits="userSpaceOnUse" width="6" height="6"&gt;&lt;path opacity="0.8" fill="none" stroke="#1498e7" stroke-width="1" d="M 0 0 L 1 1" /&gt;&lt;path opacity="0.8" fill="none" stroke="#1498e7" stroke-width="1" d="M 5 5 L 6 6" /&gt;&lt;path fill="none" stroke="#1498e7" stroke-width="2" d="M 0 6 L 6 0" /&gt;&lt;/pattern&gt;&lt;pattern id="DevExpressPattern_47" patternUnits="userSpaceOnUse" width="6" height="6"&gt;&lt;path opacity="0.8" fill="none" stroke="#7bc940" stroke-width="1" d="M 0 0 L 1 1" /&gt;&lt;path opacity="0.8" fill="none" stroke="#7bc940" stroke-width="1" d="M 5 5 L 6 6" /&gt;&lt;path fill="none" stroke="#7bc940" stroke-width="2" d="M 0 6 L 6 0" /&gt;&lt;/pattern&gt;&lt;pattern id="DevExpressPattern_48" patternUnits="userSpaceOnUse" width="6" height="6"&gt;&lt;path opacity="0.8" fill="none" stroke="#c7399a" stroke-width="1" d="M 0 0 L 1 1" /&gt;&lt;path opacity="0.8" fill="none" stroke="#c7399a" stroke-width="1" d="M 5 5 L 6 6" /&gt;&lt;path fill="none" stroke="#c7399a" stroke-width="2" d="M 0 6 L 6 0" /&gt;&lt;/pattern&gt;&lt;/defs&gt;&lt;g class="series"&gt;&lt;g class="series"&gt;&lt;path fill="#0084d3" stroke="#ffffff" stroke-dasharray="none" stroke-linejoin="round" stroke-width="2" d="M 85.9999 229.674 A 92 92 0 0 0 224 150 L 132 150 A 0 0 0 0 1 132 150 Z" r="0" line-width="2" /&gt;&lt;path fill="#67b52c" stroke="#ffffff" stroke-dasharray="none" stroke-linejoin="round" stroke-width="2" d="M 86 70.3256 A 92 92 0 0 0 85.9999 229.674 L 132 150 A 0 0 0 0 1 132 150 Z" r="0" line-width="2" /&gt;&lt;path fill="#b32586" stroke="#ffffff" stroke-dasharray="none" stroke-linejoin="round" stroke-width="2" d="M 224 150 A 92 92 0 0 0 86 70.3256 L 132 150 A 0 0 0 0 1 132 150 Z" r="0" line-width="2" /&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class="labels"&gt;&lt;g class="labels"&gt;&lt;g&gt;&lt;path fill="none" stroke="#0084d3" stroke-width="1" d="M 179 231 L 188 247" /&gt;&lt;g transform="rotate(0)" x="187.9998589266569" y="246.99492667245295"&gt;&lt;rect fill="#0084d3" stroke="none" stroke-dasharray="none" stroke-width="0" x="185" y="243" width="43" height="25" rx="0" ry="0" /&gt;&lt;text style="font-family: 'SegoeUI', 'Segoe UI', 'HelveticaNeue', 'Helvetica Neue', 'Trebuchet MS', Verdana; font-size: 12px; font-weight: 400; fill: #ffffff;" text-anchor="start" x="192.9999" y="260"&gt;&lt;tspan x="192.9999" dy="0"&gt;33 %&lt;/tspan&gt;&lt;/text&gt;&lt;/g&gt;&lt;/g&gt;&lt;g&gt;&lt;path fill="none" stroke="#67b52c" stroke-width="1" d="M 39 150.5 L 20 150.5" /&gt;&lt;g transform="translate(6) rotate(0)" x="20.000000000042646" y="149.99990226156188"&gt;&lt;rect fill="#67b52c" stroke="none" stroke-dasharray="none" stroke-width="0" x="-6" y="137" width="25" height="25" rx="0" ry="0" /&gt;&lt;text style="font-family: 'SegoeUI', 'Segoe UI', 'HelveticaNeue', 'Helvetica Neue', 'Trebuchet MS', Verdana; font-size: 12px; font-weight: 400; fill: #ffffff;" text-anchor="end" x="10" y="154"&gt;&lt;tspan x="10" dy="0"&gt;...&lt;/tspan&gt;&lt;/text&gt;&lt;/g&gt;&lt;/g&gt;&lt;g&gt;&lt;path fill="none" stroke="#b32586" stroke-width="1" d="M 179 69 L 188 53" /&gt;&lt;g transform="rotate(0)" x="188.00002821465443" y="53.00517106588667"&gt;&lt;rect fill="#b32586" stroke="none" stroke-dasharray="none" stroke-width="0" x="185" y="32" width="43" height="25" rx="0" ry="0" /&gt;&lt;text style="font-family: 'SegoeUI', 'Segoe UI', 'HelveticaNeue', 'Helvetica Neue', 'Trebuchet MS', Verdana; font-size: 12px; font-weight: 400; fill: #ffffff;" text-anchor="start" x="193" y="49"&gt;&lt;tspan x="193" dy="0"&gt;33 %&lt;/tspan&gt;&lt;/text&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class="dxTooltip"&gt;&lt;path visibility="hidden" opacity="0.1" fill="#000000" stroke="none" d="M 0 0" /&gt;&lt;path visibility="hidden" fill="black" stroke="none" d="M 0 0 Z" /&gt;&lt;text visibility="hidden" style="font-family: 'SegoeUI-Light', 'Segoe UI Light', 'HelveticaNeue-Light', 'Helvetica Neue Light', 'SegoeUI', 'Segoe UI', 'HelveticaNeue', 'Helvetica Neue', 'Trebuchet MS', Verdana; font-size: 26px; font-weight: 200; cursor: default; fill: #ffffff; fill-opacity: 0.75;" text-anchor="middle" x="0" y="0"&gt;&lt;tspan x="0" dy="0"&gt;0&lt;/tspan&gt;&lt;/text&gt;&lt;/g&gt;&lt;g class="tracker"&gt;&lt;g class="seriesTracker" /&gt;&lt;g class="markerTracker"&gt;&lt;path opacity="0.0001" fill="grey" stroke="none" stroke-linejoin="round" d="M 85.9999 229.674 A 92 92 0 0 0 224 150 L 132 150 A 0 0 0 0 1 132 150 Z" /&gt;&lt;path opacity="0.0001" fill="grey" stroke="none" stroke-linejoin="round" d="M 86 70.3256 A 92 92 0 0 0 85.9999 229.674 L 132 150 A 0 0 0 0 1 132 150 Z" /&gt;&lt;path opacity="0.0001" fill="grey" stroke="none" stroke-linejoin="round" d="M 224 150 A 92 92 0 0 0 86 70.3256 L 132 150 A 0 0 0 0 1 132 150 Z" /&gt;&lt;/g&gt;&lt;/g&gt;&lt;/svg&gt;

  1. 完全存储在 Session 中的相同变量
  2. SVG 的输出被剪切:

&lt;svg xmlns="http://www.w3.org/2000/svg" width="263" height="300" xmlns="http://www.w3.org/2000/svg" xmlns:NS1="" NS1:xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"&gt;&lt;defs&gt;&lt;clipPath id="DevExpress_28"&gt;&lt;rect fill="none" stroke="none" stroke-width="0" x="0" y="0" width="263" height="300" rx="0" ry="0" /&gt;&lt;/clipPath&gt;&lt;pattern id="DevExpressPattern_46" patternUnits="userSpaceOnUse" width="6" height="6"&gt;&lt;path opacity="0.8" fill="none" stroke="#1498e7" stroke-width="1" d="M 0 0 L 1 1" /&gt;&lt;path opacity="0.8" fill="none" stroke="#1498e7" stroke-width="1" d="M 5 5 L 6 6" /&gt;&lt;path fill="none" stroke="#1498e7" stroke-width="2" d="M 0 6 L 6 0" /&gt;&lt;/pattern&gt;&lt;pattern id="DevExpressPattern_47" patternUnits="userSpaceOnUse" width="6" height="6"&gt;&lt;path opacity="0.8" fill="none" stroke="#7bc940" stroke-width="1" d="M 0 0 L 1 1" /&gt;&lt;path opacity="0.8" fill="none" stroke="#7bc940" stroke-width="1" d="M 5 5 L 6 6" /&gt;&lt;path fill="none" stroke="#7bc940" stroke-width="2" d="M 0 6 L 6 0" /&gt;&lt;/pattern&gt;&lt;pattern id="DevExpressPattern_48" patternUnits="userSpaceOnUse" width="6" height="6"&gt;&lt;path opacity="0.8" fill="none" stroke="#c7399a" stroke-width="1" d="M 0 0 L 1 1" /&gt;&lt;path opacity="0.8" fill="none" stroke="#c7399a" stroke-width="1" d="M 5 5 L 6 6" /&gt;&lt;path fill="none" stroke="#c7399a" stroke-width="2" d="M 0 6 L 6 0" /&gt;&lt;/pattern&gt;&lt;/defs&gt;&lt;g class="series"&gt;&lt;g class="series"&gt;&lt;path fill="#0084d3" stroke="#ffffff" stroke-dasharray="none" stroke-linejoin="round" stroke-width="2" d="M 85.9999 229.674 A 92 92 0 0 0 224 150 L 132 150 A 0 0 0 0 1 132 150 Z" r="0" line-width="2" /&gt;&lt;path fill="#67b52c" stroke="#ffffff" stroke-dasharray="none" stroke-linejoin="round" stroke-width="2" d="M 86 70.3256 A 92 92 0 0 0 85.9999 229.674 L 132 150 A 0 0 0 0 1 132 150 Z" r="0" line-width="2" /&gt;&lt;path fill="#b32586" stroke="#ffffff" stroke-dasharray="none" stroke-linejoin="round" stroke-width="2" d="M 224 150 A 92 92 0 0 0 86 70.3256 L 132 150 A 0 0 0 0 1 132 150 Z" r="0" line-width="2" /&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class="labels"&gt;&lt;g class="labels"&gt;&lt;g&gt;&lt;path fill="none" stroke="#0084d3" stroke-width="1" d="M 179 231 L 188 247" /&gt;&lt;g transform="rotate(0)" x="187.9998589266569" y="246.99492667245295"&gt;&lt;rect fill="#0084d3" stroke="none" stroke-dasharray="none" stroke-width="0" x="185" y="243" width="43" height="25" rx="0" ry="0" /&gt;&lt;text style="font-family: 'SegoeUI', 'Segoe UI', 'HelveticaNeue', 'Helvetica Neue', 'Trebuchet MS', Verdana; font-size: 12px; font-weight: 400; fill: #ffffff;" text-anchor="start" x="192.9999" y="260"&gt;&lt;tspan x="192.9999" dy="0"&gt;33 %&lt;/tspan&gt;&lt;/text&gt;&lt;/g&gt;&lt;/g&gt;&lt;g&gt;&lt;path fill="none" stroke="#67b52c" stroke-width="1" d="M 39 150.5 L 20 150.5" /&gt;&lt;g transform="translate(6) rotate(0)" x="20.000000000042646" y="149.99990226156188"&gt;&lt;rect fill="#67b52c" stroke="none" stroke-dasharray="none" stroke-width="0" x="-6" y="137" width="25" height="25" rx="0" ry="0" /&gt;&lt;text style="font-family: 'SegoeUI', 'Segoe UI', 'HelveticaNeue', 'Helvetica Neue', 'Trebuchet MS', Verdana; font-size: 12px; font-weight: 400; fill: #ffffff;" text-anchor="end" x="10" y="154"&gt;&lt;tspan x="10" dy="0"&gt;...&lt;/tspan&gt;&lt;/text&gt;&lt;/g&gt;&lt;/g&gt;&lt;g&gt;&lt;path fill="none" stroke="#b32586" stroke-width="1" d="M 179 69 L 188 53" /&gt;&lt;g transform="rotate(0)" x="188.00002821465443" y="53.00517106588667"&gt;&lt;rect fill="#b32586" stroke="none" stroke-dasharray="none" stroke-width="0" x="185" y="32" width="43" height="25" rx="0" ry="0" /&gt;&lt;text style="font-family: 'SegoeUI', 'Segoe UI', 'HelveticaNeue', 'Helvetica Neue', 'Trebuchet MS', Verdana; font-size: 12px; font-weight: 400; fill: #ffffff;" text-anchor="start" x="193" y="49"&gt;&lt;tspan x="193" dy="0"&gt;33 %&lt;/tspan&gt;&lt;/text&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class="dxTooltip"&gt;&lt;path visibility="hidden" opacity="0.1" fill="#000000" stroke="none" d="M 0 0" /&gt;&lt;path visibility="hidden" fill="black" stroke="none" d="M 0 0 Z" /&gt;&lt;text visibility="hidden" style="font-family: 'SegoeUI-Light', 'Segoe UI Light', 'HelveticaNeue-Light', 'Helvetica Neue Light', 'SegoeUI', 'Segoe UI', 'HelveticaNeue', 'Helvetica Neue', 'Trebuchet MS', Verdana;

结论

上述概念在 chrome 和 ff 中运行良好,但在所有测试的 IE 版本 (9 / 10 / 11) 上都失败

我还尝试将值添加到隐藏的文本区域并将表单发送到 svg.php(效果相同 / Chrome 和 FF - 好的 / IE 失败)。

在 $.post-Request 中我有什么需要调整的吗,或者还有其他概念可以导出内联 SVG 对象吗?

解决方案(感谢 timclutton - answer

由于在开始标记中双重定义xmlns-attributes 导致的问题。因为我对创建 svg-object 的方法没有影响,所以一个简单的str_replace 完成了这项工作:

svg.php

<?
session_start();
header("Content-type: image/svg+xml");
echo str_replace("xmlns=\"http://www.w3.org/2000/svg\" xmlns:NS1=\"\" NS1:xmlns:xlink=\"http://www.w3.org/1999/xlink\"","",$_SESSION["svg"]);
?>

【问题讨论】:

  • 您需要更清楚问题是什么——“在任何地方切断字符串”是什么意思?代码“失败”是什么意思?我们无法帮助调试一个不清楚的问题。
  • 我会更新我的问题以更清楚。
  • 你能不能也给我们一个 $_SESSION["svg"] 值的例子?
  • 谢谢,@godesign。一旦细节更加充实,我会尽力帮助弄清楚!
  • @Filo 感谢您的快速反馈,我已尝试在出现问题的地方复制一些原始数据。 (大部分都是相当敏感的数据,所以我只好找一些不重要的宣传)

标签: javascript php internet-explorer svg


【解决方案1】:

我已根据您发布的代码(稍作改动)构建了以下代码,这适用于我在 Win8.1 x64 上的 IE11 和 FF30 中。也许尝试将这两个文件放在一个文件夹中(使用 jQuery,或将 src 更改为 CDN),看看它是否适合您。

文件 svg.php:

<?php
session_start();

if ($_POST) {
    $_SESSION['svg'] = $_POST['svg'];
    exit;
}

?>

<html>
    <head>
        <title>blah</title>
        <script type="text/javascript" src="jquery-1.11.1.min.js"></script>
        <script type="text/javascript">
            function svg(e) {
                var $svg = $(e).parent().find("svg"); // works fine
                var s = new XMLSerializer();
                var svgContent = s.serializeToString($svg[0]);
                $.post("svg.php", // saveSVG => store SVG-Data in Session
                    { "svg" : svgContent }).done(function(data) {
                    window.open("svg2.php");
                });
            }
        </script>
    </head>
    <body>
        <div>
            <svg>
                <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
            </svg>
            <a href='#export' onclick="svg(this)">export</a>
            <!-- click on this link triggers the javascript-function !-->
        </div>
    </body>
</html>

文件 svg2.php:

<?php
session_start();
header("Content-type: image/svg+xml");
echo $_SESSION["svg"];
exit;
?>

实际 SVG 内容的更新

问题似乎出在开始标签上。将其从...更改为...

<svg xmlns="http://www.w3.org/2000/svg" width="263" height="300" xmlns="http://www.w3.org/2000/svg" xmlns:NS1="" NS1:xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">

...到...

<svg xmlns="http://www.w3.org/2000/svg" width="263" height="300" version="1.1">

...使它与我上面发布的代码一起工作。


更新 2

通过W3C Validator 运行 SVG 代码会显示错误。使用我在上面发布的修改后的开始标签验证成功。

总之,问题在于无效的 XML。

【讨论】:

  • 这几乎就是我的做法。我在我的问题中复制了一个不敏感的 SVG 文件示例,出现问题。
  • 很好奇。在我的页面中使用您的 代码会在 IE 和 FF 中生成无效的 XML 错误。但是从第二页中删除 header("Content-type: image/svg+xml"); 会导致正确的渲染。也许这是一个不正确的内容类型?
  • 我更新了我的帖子,更改了您的 SVG 代码。
  • 是的!你说对了。非常感谢您的验证提示。我什至它不是很漂亮,但我只是在回显$_SESSION['svg'] 之前替换了打开的svg-tag 中的无效部分。这现在工作正常。你的努力真的值得(在 20 小时内)
  • 太好了,很高兴我能帮上忙。修复看起来很难看,但如果你不控制 SVG 代码生成,那么至少你找到了一个可行的解决方案!
【解决方案2】:

也许它不相关,但你试过吗?

header("Content-type: image/svg+xml");

echo '<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' .$_SESSION["svg"];

(摘自https://stackoverflow.com/a/1623265/3625883

【讨论】:

    猜你喜欢
    • 2013-11-26
    • 2019-05-24
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    相关资源
    最近更新 更多