Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70434

File download incomplete using dynamic link method

$
0
0

@leonardofmed wrote:

I have a text file and I’m doing some changes in it before the user downloads. All changes are made with TypeScript and don’t generate any errors. The problem I’m facing is that, when the user download the file, it always comes incomplete after a specific word. If I console.log before the actual download, I can see the file perfectly fine. The source of the problem seems to be a added reference to the file, because if I remove this ‘Add references’ part, the file is download as expected. Sadly I cannot remove this part.

This function was made for when the user is navigating through the browser:

myDownloadFunction() {
	...

	// Add references
	for (let feature of this.featuresToExport) {
		let idToExport = feature.id_;
		let featureColor:string = feature.values_.color;
		let featureHexColor = this.getColorByName(featureColor);

		let colorElement = '<Style id="app_style_'+idToExport+'"><IconStyle><Icon><href>https://earth.google.com/earth/rpc/cc/icon?color='+featureHexColor+'&amp;id=2000&amp;scale=4</href></Icon></IconStyle></Style>';

		// Add style element
		let indexOfDocument = final_output.indexOf("Document");
		let indexOfClosingDocument = final_output.indexOf(">", indexOfDocument) + 1;
		let output = [
			final_output.slice(0, indexOfClosingDocument), 
			colorElement, 
			final_output.slice(indexOfClosingDocument)
		].join('');

		// Add reference to style element
		let indexOfPlacemark = output.indexOf('Placemark id="' + idToExport + '"');
		let indexOfClosingPlacemark = output.indexOf(">", indexOfPlacemark) + 1;
		output = [
			output.slice(0, indexOfClosingPlacemark), 
			'<styleUrl>#app_style_'+idToExport+'</styleUrl>', 
			output.slice(indexOfClosingPlacemark)
		].join('');

		final_output = output;
	}	
	
	this.mainDoc = "data:text/json;charset=utf-8," + final_output;
	console.log(this.mainDoc); // <-- Here I can see the whole document perfectly fine	

	let link = document.createElement("a");
	link.download = this.file_name + this.file_extension;
	link.href = this.mainDoc;
	document.body.appendChild(link);
	link.click();
	document.body.removeChild(link);
	link = null;
}

All variables are correctly obtained and the file end in a word the middle of the text, without relation to any variable.

Originally the method I used for editing the file was jQuery.parseXML() and the same error happened, so I tried to change the method to this one I posted above.

I imagine that the problem may be some asynchronous step that is still in progress when the download event is triggered, but analyzing the code that was passed in I can’t see any asynchronous part.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70434

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>