如何确定为什么文件无法在javascript中下载?

时间:2019-08-26 11:28:39

标签: javascript file svg download kml

我正在转换SVG文件,因此我无法处理最后一步。有些文件被下载,而另一些则没有。崇高文字无法识别fr.onload()函数的范围。我不确定这是语法错误还是冗长的代码。我也不确定字符串/文件是否可以处理最大大小。转换后,我的文件约为3-5 MB。这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>Reader</title>
</head>
<body>
	<h1>SVG paths to polygons</h1>
	<input type="file" id="fileReader" />
	<br>
	<p id="Content"></p>
	<script>
		document.getElementById("fileReader").addEventListener('change', function(){
			var fr = new FileReader();
			fr.onload = function(){;
				var d = new DOMParser().parseFromString( this.result.toString(), "image/svg+xml" );
				var textOut = "Start of file";
                //Covnersion done here.
function download(filename, text) {
	var element = document.createElement('a');
	element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
	element.setAttribute('download', filename);

	element.style.display = 'none';
	document.body.appendChild(element);

	element.click();

	document.body.removeChild(element);

	//console.log("Downloaded" + filename + "!");
}
// Starting file download.
download("output.txt", textOut);
}
fr.readAsText(this.files[0]);
})
</script>
</body>
</html>

0 个答案:

没有答案