How to use anchor tag with href to download a file from local in streamlit markdown?

Summary

I have a file named “Test_Email.msg” within my project root folder. I am using st.markdown (html/css) for my web page. I want to download this file using Download
But it throws an error message and doesn’t work.

Steps to reproduce

Code snippet:

st.markdown("""
<head>
</head>
<body>
<a href='Test_Email.msg'><button>Download</button></a> 
</body>
""", unsafe_allow_html = True)

The file should download from the root folder.

Actual behavior:
It just opens a new tab saying no such file

Hi @Devansh_Popat, welcome to the forum! You’ll need to access that file through the Static Files API

So, you should be able to do app/static/Test_Email.msg once you have enabled static file serving for your app.

1 Like

Hi @blackary thank you so much for the suggestion. I was able to follow those steps but unfortunately when I try to hit the Download button, It opens up a bit chunk of garbage values in a new tab. I have uploaded the image. Do I need any additional step here to decode etc?
FYI - I am trying it on a JPG image file as per the cat image example in link you provided.

For more context, When I tried to use this line -

<img src="app/static/GroupPhoto.jpg">

That also gave me just one small icon on the web page -
image

Hey @blackary , I actually restarted my app from scratch after uninstalling streamlit and it worked for me! Thank you so much for the guidance!!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.