Skip to content
Advertisement

Hashtag character makes file incomplete when downloading

I have a text file and I’m doing some changes in it before the user downloads. All changes are made with Javascript/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 and unrelated word. If I console.log before the actual download, I can see the file perfectly fine. The source of the problem seems to be an 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:

JavaScript

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.


I tried to use FileSaver.js as an alternative method to download the file, but the same error happened.


I tried to encapsulate this part in a Promise to be sure nothing was being left behind, but this didn’t solve the issue either.

JavaScript

Here you can see an example of how the file is suppose to be, and here how is being downloaded.


I have tried some other things and I think I have narrowed down the source of the problem. When I remove the hashtag character (#) from the reference text, everything works as expected. If I leave the hashtag it breaks. Someone has a clue why this is happening? I tried to escape as we usually do (#) but that didn’t work.

JavaScript

Advertisement

Answer

Solved the problem by using the ASCII encoding reference for the hashtag character:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement