Download SVG element from page when user clicks.

huangapple go评论50阅读模式
英文:

Download SVG element from page when user clicks

问题

我创建了一个 Svelte 组件,它根据一些参数生成了一些 SVG 元素。我想为每个创建的 SVG 元素提供一个下载链接或按钮,以将 SVG 代码保存到用户计算机上的文件中。

我找到了使用 blob 和 URL.createObjectURL 的解决方案,如果我创建一个带有点击处理程序的静态 HTML 页面,它可以正常工作:

<button onclick="download(50)">Download SVG</button>

然而,在 Svelte 中相同的解决方案不会触发下载:

<script>
    const sizes = [20, 50, 90];
    
    function download(id) {
        const svg = document.getElementById(id);
        const blob = new Blob([svg.outerHTML], {type: 'image/svg+xml'});
        const link = document.createElement('a');
        link.href = URL.createObjectURL(blob);
        link.download = `文件名 ${id}.svg`;
        link.click();

        console.log(svg.outerHTML);
        console.log(blob);
        console.log(link);
    }
</script>

{#each sizes as s}
<p>
    <!-- 这个按钮不起作用 -->
    <button on:click={() => download(s)}>Download SVG</button>
    <br>
    <svg id={s} width="200" height="200" xmlns="http://www.w3.org/2000/svg">
        <ellipse cx="100" cy="100" rx={s} ry={s*0.7} fill="orange" stroke="black"/>
    </svg>
</p>
<hr/>
{/each}
英文:

I made a Svelte component, which generates a couple of SVG elements, based on some parameters. I want to provide a download-link or -button for each created SVG element, which saves the SVG code to a file on the user's computer.

I found the solution using blob and URL.createObjectURL suitable. It works if I create a static HTML page with a click handler like so:

&lt;button onclick=&quot;download(50)&quot;&gt;Download SVG&lt;/button&gt;

However the same solution in Svelte does not trigger a download:

&lt;script&gt;
	const sizes = [20, 50, 90];
	
	function download(id) {
		const svg = document.getElementById(id);
		const blob = new Blob([svg.outerHTML], {type: &#39;image/svg+xml&#39;});
		const link = document.createElement(&#39;a&#39;);
		link.href = URL.createObjectURL(blob);
		link.download = `File name ${id}.svg`;
		link.click();

		console.log(svg.outerHTML);
		console.log(blob);
		console.log(link);
	}
&lt;/script&gt;


{#each sizes as s}
&lt;p&gt;
	&lt;!-- this button does not work --&gt;
	&lt;button on:click={() =&gt; download(s)}&gt;Download SVG&lt;/button&gt;
	&lt;br&gt;
	&lt;svg id={s} width=&quot;200&quot; height=&quot;200&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
		&lt;ellipse cx=&quot;100&quot; cy=&quot;100&quot; rx={s} ry={s*0.7} fill=&quot;orange&quot; stroke=&quot;black&quot;/&gt;
	&lt;/svg&gt;
&lt;/p&gt;
&lt;hr/&gt;
{/each}

答案1

得分: 1

你不能在 REPL 中启动下载,因为它使用 iframe 来隔离输出。这在独立的 Svelte 应用程序中可以正常工作。

英文:

You cannot start downloads in the REPL because it uses an iframe to isolate the output.

This works as expected in a standalone Svelte application.

huangapple
  • 本文由 发表于 2023年5月22日 23:29:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定