Double-extension in uploaded file name?

I’m selecting a file named “test.jpg” but after selecting it, the app reports the name as ‘test.jpg.jpg’ and proceeds to upload it with the file name ‘test.jpg.jpg’.

Here’s the code - any ideas?

import streamlit as st
import pysftp
import logging

def setup_logging():
    logging.basicConfig(filename='app.log', level=logging.INFO)

def sftp_upload(file, server, port, username, password):
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None  # Disable host key checking
    try:
        with pysftp.Connection(
                host=server, 
                username=username, 
                password=password, 
                port=port,
                cnopts=cnopts) as sftp:
            remote_file_path = f'/images/{file.name}'  # specify the remote file path correctly
            st.write(f'Uploading {file.name} as {remote_file_path}')
            sftp.putfo(file, remote_file_path)  # upload file from file object
        return True
    except Exception as e:
        logging.error(f"Error: {e}")
        st.write(f"Error: {e}")
        st.write(f'Failed to upload {file.name} as {remote_file_path}') 
        return False

def main():
    setup_logging()
    st.title("SFTP File Uploader")
    uploaded_files = st.file_uploader("Choose a file to upload", type=['jpg','png'], accept_multiple_files=True)
    
    if uploaded_files:
        if st.button("Upload to SFTP"):
            server = "xyz.com"
            port = 22
            username = st.secrets["SFTP_USER"]
            password = st.secrets["SFTP_PW"]

            if not username or not password:
                st.write("Error: Missing SFTP_USER or SFTP_PW in secrets.")
                return

            for uploaded_file in uploaded_files:  # iterate over the list of uploaded files
                success = sftp_upload(uploaded_file, server, port, username, password)
                if success:
                    st.success(f'File {uploaded_file.name} uploaded successfully!')
                else:
                    st.error(f'Failed to upload the file {uploaded_file.name}.')

if __name__ == "__main__":
    main()

If you are using Windows, note that by default it doesn’t show you the extensions of most files. So if you have a file test.jpg.jpg, Windows would show it as just test.jpg.

Sigh. Yes, I just had to reinstall Windows and didn’t have “Show extensions” enabled, so I was renaming it to .jpg.jpg … Thank you, that was driving me nuts.

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