How to save a text as .txt file via a downloadable link?

I am working on an app using Streamlit. Based on the user inputs to the fields available, I am generating some data which I want to download in the form of a .txt file.

The data that I want to download is generated when I do

to_save = abc.serialize().encode("ascii", "ignore")

and when I do print(to_save), I get (this is just a small part of a very huge text data)

b"UNA:+.?
‘UNB+UNOC:3+9978715000006:14+9978715000006:14+200529:1139+50582307060_WP?+_200101_200201++TL’UNH+1+MSCONS:D:04B:UN:2.3’BGM+7+50582307060_WP?+_200101_200201-1+9’DTM+137:202005291139:203’RFF+Z13:13008’NAD+MS+9978715000006::9’CTA+IC+:Michael
Jordan’COM+m.jordan@energycortex.com:EM’NAD+MR+9978715000006::9’"

Now, I want to save this information as a .txt file via an HTML link. I am following:

  1. How to download a file in Streamlit
  2. How to force fully download txt file on link?

and I have

reference = 50582307060_WP+_200101_200201
to_save = abc.serialize().encode("ascii", "ignore")
href = f'<a href="data:text/plain;charset=UTF-8,{to_save}" download={reference}.txt>Download File</a> (right-click and save as {reference}.txt)'
st.markdown(href, unsafe_allow_html=True)

But this doesn’t work and shows as follows:

The start

enter image description here

The end

enter image description here

and when I do:

to_save = abc.serialize().encode("ascii", "ignore")
href = f'<a href="data:text/plain;charset=UTF-8" download={reference}.txt>Download File</a> (right-click and save as {reference}.txt)'
st.markdown(href, unsafe_allow_html=True)

I get

enter image description here

The problem with this being that the information that has to be saved as a .txt file (to_save = abc.serialize().encode("ascii", "ignore")) isn’t being saved and I get a Failed-Network error

What is the mistake that I am doing and how can I enable saving the information stored in to_save (to_save = abc.serialize().encode("ascii", "ignore")) as an HTML downloadable link? Also, the file should be saved as ‘reference.txt’, with reference being defined as a variable above.

Hi @Junkrat, sorry for missing this one. It looks like you got the answer on SO, which I’ll copy here for anyone coming across it in the future: