Get path from file_uploader()

I need to be able to retrieve the path and filename based on the user’s selection from the file_uploader. Having this information would allow me to update a json config file for future session automatic file loading.

Thank you for any advise.

greg

4 Likes

Hi @555gam, welcome to the Streamlit community!

When the file_uploader widget is used, no file is actually created. The return value is a BytesIO object, which stays in RAM. If you’d like to save that file somewhere, you can choose to do so.

Best,
Randy

1 Like

Thank you for the welcome, Randy.

I understand the current operation of the file_uploader widget and I am using it in my app now! What I would like to have, in addition to the StringIO (in my case), is the path & file name that was selected by the user. The current file_uploader() does not appear to support such a return option but I wanted to find out if there were some other way. Or, how do I go about making an enhancement request to the library (I am new to this so please bear with me). Thank you for your quick reply!

greg

I am having the same issue.

1 Like

Having same issue with the file_uploader , can’t access the file name,… and need to send it to a flask api
two files and a string, have issue understanding how the uploader works really :roll_eyes:

1 Like

There might be issues regarding security and/or privacy. Though that feature would be useful for local apps that work with files that are already on the local file system and which do not need to be transferred. Optionally, a different function that is not called “upload” would be nice. Right now, I am using a text field that specifies the path for files I want to work with.

The uploaded does not create a file i.e. stores the data on the hard drive. Instead, it creates an in memory object that can be used as if it is is a file. You can add a function that write the file to the disc though. And here it would be nice to be able to have access to the file name and not just to its content. If I understand the issue right.

1 Like

Hi @555gam,

have you found a workaround for this, or did you just ditch the file_uploader?

Having the same issue over here :grimacing:
Especially frustrating since there is the functionality to retrieve the file name.

Best,
Francisco

Hi @fschlz @555gam,

I also need to get path of the uploaded file.

Have any of you found a workaround here?

Best,
Michal

Hey @MichalBZ,

Check out this link.
It shows how to use the base64 package and HTML to solve the problem.

Best
Francisco

If you do not want the full path (to check extension for example), you can just do :

        import streamlit as st
    
        f = st.file_uploader("Upload a file", type=(["tsv","csv","txt","tab","xlsx","xls"]))
        if f is not None:
            path_in = f.name
            print(path_in)
        else:
            path_in = None

Hi,
is there any update to get the file path in file_uploader. I want to send the file path to the post request.
Please let me know any workaround to get file path.

Hi everyone,

for me it seems that this is still an unsolved issue. Some functions and packages require to specify a file path as the input. I have not found a work around for such cases apart of i) uploading the file; ii) storing it remotely, for instance in a cloud storage, iii) getting the path from the cloud storage, iv) input the file path in the function.

We should have an option to not only get bytes by file.getvalues() or file.read() but something like file.path().

Hi @jonas-nothnagel :wave:

I would suggest giving a thumbs up to this feature request and commenting your support and use-case for the same:

In the meantime, for server-side file selection, I would suggest something like this snippet Upload Files to Streamlit App - #3 by Adrien_Treuille

2 Likes

Check these out!
StackOverflow
Streamlit Discuss

Other than uploading to cloud, there is also the very useful tempfile module including the NamedTemporaryFileclass.

Something along the lines of:

from tempfile import NamedTemporaryFile
import streamlit as st

uploaded_file = st.file_uploader("File upload", type='csv')
with NamedTemporaryFile(dir='.', suffix='.csv') as f:
    f.write(uploaded_file.getbuffer())
    your_function_which_takes_a_path(f.name)

Should work.

9 Likes

Thanks for your comment. It worked for me :slight_smile:

1 Like

In my case, I also created a tempfile.NamedTemporaryFile like @ennui suggested. Good thing about it is, tempfile is in the standard library, so no more hassle. However, delete=False was necessary to open the temp file again. See this Stack Overflow answer for more details.

can you find out the path of the uploaded file, but not name?