Download file without download button

Hi,
is there a way to download a file without using the download button? I have a button, that, if clicked, runs some different commands and one of them is downloading a file. So Iโ€™m using a st.button, I canโ€™t use a st.download_button because I must perform different tasks at once.

How can I do that?

1 Like

In this thread there are maybe some ideas?

1 Like

@Franky1 Thanks. This solved the issue: Automatic Download / Select and Download File with Single Button Click - #4 by snehankekre

1 Like

I use streamlit to create web application. In addition task I need to recieve PDF file via SOAP connection and download it throw browser. Here the function in pure JavaScript to download file without nature download streamlit button (which here unneccesary unfortutanelly)

def download_base64_file(b64, download_filename):
    id_link = '_'+str(uuid.uuid4())
    components.html(
        f"""<html><body>                                   
        <a href="data:application/pdf;base64,{b64}" download="{download_filename}" id="{id_link}"></a>""" +
        """<script>                                    
                window.onload = function () {                                            
                    document.getElementById('""" + id_link + """').click();
                                    };                                        
                                </script>
                            </body></html>                                    
                            """, height=0, width=0)

here b64 is bynary PDF file encrypted using base64 algoritm (type - string). download_filename - string file name to download. Dont foget import follow vibraries:
import streamlit.components.v1 as components and import uuid

1 Like