This works fine:
<html>
<script>
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' encodeURIComponent('xml string value'));
pom.setAttribute('download', 'fire.txt');
pom.click();
</script>
<body>
</body>
</html>
However when I try it with an XML string, I get this error even after I replace the illegal characters as per the instructions on page how to escape xml entities in javascript? :
Uncaught SyntaxError: missing ) after argument list
XML string:
<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.518" Build="13.0.6404.1"><BatchSequence><Batch><Statements><StmtSimple StatementText="(@P0 bigint,@P1 nvarchar(4000))select
 *
 from
 name_lock_resource
 where
 qname_ns_id = @P0 and
 qname_localname = @P1" StatementId="1" StatementCompId="1" StatementType="SELECT" RetrievedFromCache="true" StatementSubTreeCost="0.00657038" StatementEstRows="1" SecurityPolicyApplied="false" StatementOptmLevel="TRIVIAL" QueryHash="0xF166488BCBB26747" QueryPlanHash="0x5445E6055A7EA8E1" CardinalityEstimationModelVersion="130"><StatementSetOptions QUOTED_IDENTIFIER="true" ARITHABORT="false" CONCAT_NULL_YIELDS_NULL="true" ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" NUMERIC_ROUNDABORT="false"/><QueryPlan CachedPlanSize="24" CompileTime="3" CompileCPU="0" CompileMemory="144"><MemoryGrantInfo SerialRequiredMemory="0" SerialDesiredMemory="0"/><OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="524288" EstimatedPagesCached="131072" EstimatedAvailableDegreeOfParallelism="2" MaxCompileMemory="15500928"/><TraceFlags IsCompileTime="1"><TraceFlag Value="460" Scope="Global"/><TraceFlag Value="1118" Scope="Global"/><TraceFlag Value="2371" Scope="Global"/><TraceFlag Value="3023" Scope="Global"/><TraceFlag Value="3226" Scope="Global"/><TraceFlag Value="4199" Scope="Global"/><TraceFlag Value="7412" Scope="Global"/></TraceFlags><RelOp NodeId="0" PhysicalOp="Nested Loops" LogicalOp="Inner Join" EstimateRows="1" EstimateIO="0" EstimateCPU="4.18e-006" AvgRowSize="293" EstimatedTotalSubtreeCost="0.00657038" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="version"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></OutputList><NestedLoops Optimized="0"><OuterReferences><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></OuterReferences><RelOp NodeId="1" PhysicalOp="Index Seek" LogicalOp="Index Seek" EstimateRows="1" EstimatedRowsRead="1" EstimateIO="0.003125" EstimateCPU="0.0001581" AvgRowSize="284" EstimatedTotalSubtreeCost="0.0032831" TableCardinality="32" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></OutputList><IndexScan Ordered="1" ScanDirection="FORWARD" ForcedIndex="0" ForceSeek="0" ForceScan="0" NoExpandHint="0" Storage="RowStore"><DefinedValues><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></DefinedValue><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/></DefinedValue><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></DefinedValue></DefinedValues><Object Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Index="[idx_name_lockr_key]" IndexKind="NonClustered" Storage="RowStore"/><SeekPredicates><SeekPredicateNew><SeekKeys><Prefix ScanType="EQ"><RangeColumns><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></RangeColumns><RangeExpressions><ScalarOperator ScalarString="CONVERT_IMPLICIT(numeric(19,0),[@P0],0)"><Convert DataType="numeric" Precision="19" Scale="0" Style="0" Implicit="1"><ScalarOperator><Identifier><ColumnReference Column="@P0"/></Identifier></ScalarOperator></Convert></ScalarOperator><ScalarOperator ScalarString="[@P1]"><Identifier><ColumnReference Column="@P1"/></Identifier></ScalarOperator></RangeExpressions></Prefix></SeekKeys></SeekPredicateNew></SeekPredicates></IndexScan></RelOp><RelOp NodeId="3" PhysicalOp="Clustered Index Seek" LogicalOp="Clustered Index Seek" EstimateRows="1" EstimateIO="0.003125" EstimateCPU="0.0001581" AvgRowSize="16" EstimatedTotalSubtreeCost="0.0032831" TableCardinality="32" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="version"/></OutputList><IndexScan Lookup="1" Ordered="1" ScanDirection="FORWARD" ForcedIndex="0" ForceSeek="0" ForceScan="0" NoExpandHint="0" Storage="RowStore"><DefinedValues><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="version"/></DefinedValue></DefinedValues><Object Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Index="[PK__name_lock__3213E83FDF395D04]" TableReferenceId="-1" IndexKind="Clustered" Storage="RowStore"/><SeekPredicates><SeekPredicateNew><SeekKeys><Prefix ScanType="EQ"><RangeColumns><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></RangeColumns><RangeExpressions><ScalarOperator ScalarString="[namedb_test].[dbo].[name_lock_resource].[id]"><Identifier><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></Identifier></ScalarOperator></RangeExpressions></Prefix></SeekKeys></SeekPredicateNew></SeekPredicates></IndexScan></RelOp></NestedLoops></RelOp><ParameterList><ColumnReference Column="@P1" ParameterDataType="nvarchar(4000)" ParameterCompiledValue="N'feedgenerator'"/><ColumnReference Column="@P0" ParameterDataType="bigint" ParameterCompiledValue="(1)"/></ParameterList></QueryPlan></StmtSimple></Statements></Batch></BatchSequence></ShowPlanXML>
Code that produces the error whether I escape XML characters or not:
<html>
<script>
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' encodeURIComponent('<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.518" Build="13.0.6404.1"><BatchSequence><Batch><Statements><StmtSimple StatementText="(@P0 bigint,@P1 nvarchar(4000))select
 *
 from
 name_lock_resource
 where
 qname_ns_id = @P0 and
 qname_localname = @P1" StatementId="1" StatementCompId="1" StatementType="SELECT" RetrievedFromCache="true" StatementSubTreeCost="0.00657038" StatementEstRows="1" SecurityPolicyApplied="false" StatementOptmLevel="TRIVIAL" QueryHash="0xF166488BCBB26747" QueryPlanHash="0x5445E6055A7EA8E1" CardinalityEstimationModelVersion="130"><StatementSetOptions QUOTED_IDENTIFIER="true" ARITHABORT="false" CONCAT_NULL_YIELDS_NULL="true" ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" NUMERIC_ROUNDABORT="false"/><QueryPlan CachedPlanSize="24" CompileTime="3" CompileCPU="0" CompileMemory="144"><MemoryGrantInfo SerialRequiredMemory="0" SerialDesiredMemory="0"/><OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="524288" EstimatedPagesCached="131072" EstimatedAvailableDegreeOfParallelism="2" MaxCompileMemory="15500928"/><TraceFlags IsCompileTime="1"><TraceFlag Value="460" Scope="Global"/><TraceFlag Value="1118" Scope="Global"/><TraceFlag Value="2371" Scope="Global"/><TraceFlag Value="3023" Scope="Global"/><TraceFlag Value="3226" Scope="Global"/><TraceFlag Value="4199" Scope="Global"/><TraceFlag Value="7412" Scope="Global"/></TraceFlags><RelOp NodeId="0" PhysicalOp="Nested Loops" LogicalOp="Inner Join" EstimateRows="1" EstimateIO="0" EstimateCPU="4.18e-006" AvgRowSize="293" EstimatedTotalSubtreeCost="0.00657038" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="version"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></OutputList><NestedLoops Optimized="0"><OuterReferences><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></OuterReferences><RelOp NodeId="1" PhysicalOp="Index Seek" LogicalOp="Index Seek" EstimateRows="1" EstimatedRowsRead="1" EstimateIO="0.003125" EstimateCPU="0.0001581" AvgRowSize="284" EstimatedTotalSubtreeCost="0.0032831" TableCardinality="32" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></OutputList><IndexScan Ordered="1" ScanDirection="FORWARD" ForcedIndex="0" ForceSeek="0" ForceScan="0" NoExpandHint="0" Storage="RowStore"><DefinedValues><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></DefinedValue><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/></DefinedValue><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></DefinedValue></DefinedValues><Object Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Index="[idx_name_lockr_key]" IndexKind="NonClustered" Storage="RowStore"/><SeekPredicates><SeekPredicateNew><SeekKeys><Prefix ScanType="EQ"><RangeColumns><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></RangeColumns><RangeExpressions><ScalarOperator ScalarString="CONVERT_IMPLICIT(numeric(19,0),[@P0],0)"><Convert DataType="numeric" Precision="19" Scale="0" Style="0" Implicit="1"><ScalarOperator><Identifier><ColumnReference Column="@P0"/></Identifier></ScalarOperator></Convert></ScalarOperator><ScalarOperator ScalarString="[@P1]"><Identifier><ColumnReference Column="@P1"/></Identifier></ScalarOperator></RangeExpressions></Prefix></SeekKeys></SeekPredicateNew></SeekPredicates></IndexScan></RelOp><RelOp NodeId="3" PhysicalOp="Clustered Index Seek" LogicalOp="Clustered Index Seek" EstimateRows="1" EstimateIO="0.003125" EstimateCPU="0.0001581" AvgRowSize="16" EstimatedTotalSubtreeCost="0.0032831" TableCardinality="32" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="version"/></OutputList><IndexScan Lookup="1" Ordered="1" ScanDirection="FORWARD" ForcedIndex="0" ForceSeek="0" ForceScan="0" NoExpandHint="0" Storage="RowStore"><DefinedValues><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="version"/></DefinedValue></DefinedValues><Object Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Index="[PK__name_lock__3213E83FDF395D04]" TableReferenceId="-1" IndexKind="Clustered" Storage="RowStore"/><SeekPredicates><SeekPredicateNew><SeekKeys><Prefix ScanType="EQ"><RangeColumns><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></RangeColumns><RangeExpressions><ScalarOperator ScalarString="[namedb_test].[dbo].[name_lock_resource].[id]"><Identifier><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></Identifier></ScalarOperator></RangeExpressions></Prefix></SeekKeys></SeekPredicateNew></SeekPredicates></IndexScan></RelOp></NestedLoops></RelOp><ParameterList><ColumnReference Column="@P1" ParameterDataType="nvarchar(4000)" ParameterCompiledValue="N''feedgenerator''"/><ColumnReference Column="@P0" ParameterDataType="bigint" ParameterCompiledValue="(1)"/></ParameterList></QueryPlan></StmtSimple></Statements></Batch></BatchSequence></ShowPlanXML>'));
pom.setAttribute('download', 'fire.txt');
pom.click();
</script>
<body>
</body>
What am I doing wrong?
CodePudding user response:
I tried the following example:
var xml = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
</xsl:stylesheet>`;
var link = document.createElement('a');
link.textContent = 'XML Download';
link.href = 'data:application/xml;charset=UTF-8,' encodeURIComponent(xml);
link.setAttribute('download', 'sheet1.xsl');
document.body.appendChild(link);
Clicking the link then works for me, although the sandbox settings of the StackOverflow snippet sample seem to prevent it from working directly in this page.
For your more complex data, String.raw
can help:
var pom = document.createElement('a');
pom.textContent = 'Download link';
pom.href = 'data:text/plain;charset=utf-8,' encodeURIComponent(String.raw`<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.518" Build="13.0.6404.1"><BatchSequence><Batch><Statements><StmtSimple StatementText="(@P0 bigint,@P1 nvarchar(4000))select
 *
 from
 name_lock_resource
 where
 qname_ns_id = @P0 and
 qname_localname = @P1" StatementId="1" StatementCompId="1" StatementType="SELECT" RetrievedFromCache="true" StatementSubTreeCost="0.00657038" StatementEstRows="1" SecurityPolicyApplied="false" StatementOptmLevel="TRIVIAL" QueryHash="0xF166488BCBB26747" QueryPlanHash="0x5445E6055A7EA8E1" CardinalityEstimationModelVersion="130"><StatementSetOptions QUOTED_IDENTIFIER="true" ARITHABORT="false" CONCAT_NULL_YIELDS_NULL="true" ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" NUMERIC_ROUNDABORT="false"/><QueryPlan CachedPlanSize="24" CompileTime="3" CompileCPU="0" CompileMemory="144"><MemoryGrantInfo SerialRequiredMemory="0" SerialDesiredMemory="0"/><OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="524288" EstimatedPagesCached="131072" EstimatedAvailableDegreeOfParallelism="2" MaxCompileMemory="15500928"/><TraceFlags IsCompileTime="1"><TraceFlag Value="460" Scope="Global"/><TraceFlag Value="1118" Scope="Global"/><TraceFlag Value="2371" Scope="Global"/><TraceFlag Value="3023" Scope="Global"/><TraceFlag Value="3226" Scope="Global"/><TraceFlag Value="4199" Scope="Global"/><TraceFlag Value="7412" Scope="Global"/></TraceFlags><RelOp NodeId="0" PhysicalOp="Nested Loops" LogicalOp="Inner Join" EstimateRows="1" EstimateIO="0" EstimateCPU="4.18e-006" AvgRowSize="293" EstimatedTotalSubtreeCost="0.00657038" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="version"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></OutputList><NestedLoops Optimized="0"><OuterReferences><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></OuterReferences><RelOp NodeId="1" PhysicalOp="Index Seek" LogicalOp="Index Seek" EstimateRows="1" EstimatedRowsRead="1" EstimateIO="0.003125" EstimateCPU="0.0001581" AvgRowSize="284" EstimatedTotalSubtreeCost="0.0032831" TableCardinality="32" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></OutputList><IndexScan Ordered="1" ScanDirection="FORWARD" ForcedIndex="0" ForceSeek="0" ForceScan="0" NoExpandHint="0" Storage="RowStore"><DefinedValues><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></DefinedValue><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/></DefinedValue><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></DefinedValue></DefinedValues><Object Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Index="[idx_name_lockr_key]" IndexKind="NonClustered" Storage="RowStore"/><SeekPredicates><SeekPredicateNew><SeekKeys><Prefix ScanType="EQ"><RangeColumns><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_ns_id"/><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="qname_localname"/></RangeColumns><RangeExpressions><ScalarOperator ScalarString="CONVERT_IMPLICIT(numeric(19,0),[@P0],0)"><Convert DataType="numeric" Precision="19" Scale="0" Style="0" Implicit="1"><ScalarOperator><Identifier><ColumnReference Column="@P0"/></Identifier></ScalarOperator></Convert></ScalarOperator><ScalarOperator ScalarString="[@P1]"><Identifier><ColumnReference Column="@P1"/></Identifier></ScalarOperator></RangeExpressions></Prefix></SeekKeys></SeekPredicateNew></SeekPredicates></IndexScan></RelOp><RelOp NodeId="3" PhysicalOp="Clustered Index Seek" LogicalOp="Clustered Index Seek" EstimateRows="1" EstimateIO="0.003125" EstimateCPU="0.0001581" AvgRowSize="16" EstimatedTotalSubtreeCost="0.0032831" TableCardinality="32" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="version"/></OutputList><IndexScan Lookup="1" Ordered="1" ScanDirection="FORWARD" ForcedIndex="0" ForceSeek="0" ForceScan="0" NoExpandHint="0" Storage="RowStore"><DefinedValues><DefinedValue><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="version"/></DefinedValue></DefinedValues><Object Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Index="[PK__name_lock__3213E83FDF395D04]" TableReferenceId="-1" IndexKind="Clustered" Storage="RowStore"/><SeekPredicates><SeekPredicateNew><SeekKeys><Prefix ScanType="EQ"><RangeColumns><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></RangeColumns><RangeExpressions><ScalarOperator ScalarString="[namedb_test].[dbo].[name_lock_resource].[id]"><Identifier><ColumnReference Database="[namedb_test]" Schema="[dbo]" Table="[name_lock_resource]" Column="id"/></Identifier></ScalarOperator></RangeExpressions></Prefix></SeekKeys></SeekPredicateNew></SeekPredicates></IndexScan></RelOp></NestedLoops></RelOp><ParameterList><ColumnReference Column="@P1" ParameterDataType="nvarchar(4000)" ParameterCompiledValue="N''feedgenerator''"/><ColumnReference Column="@P0" ParameterDataType="bigint" ParameterCompiledValue="(1)"/></ParameterList></QueryPlan></StmtSimple></Statements></Batch></BatchSequence></ShowPlanXML>`);
pom.setAttribute('download', 'fire.txt');
document.body.appendChild(pom);
pom.click();