How to get the "path" of an uploaded file?

Yet another question! :smile: Is it possible to obtain the path of a file uploaded to VIKTOR? The file I have uploaded is like a csv but contains multiple “tables”, unfortunately the getvalue() method is not useful as i am using a library to aid parsing this format. The library requires a string path or path-like object to read the file. Hopefully the above makes sense.

Hi Ian,

Thanks for this question. It is indeed the case that with most Python packages, the “normal” way of reading and accessing files is by referring to their paths. It is, however, the case that many packages provide the option to read files as memory buffers (such as BytesIO, StringIO) or formats such as string or bytes.

The File object that one retrieves from the parametrization can be use transformed to other objects by using either getvalue (for strings) or getvalue_binary (for bytes).

These can be converted to BytesIO or StringIO as follows:

from io import StringIO, BytesIO

bytes_io_object = BytesIO(uploaded_file.getvalue_binary())
string_io_object = StringIO(uploaded_file.getvalue())

To help you with your question, could you share what package you are using?

Hey there,

Just in case Marcel’s answer didn’t fully cover your case: have you perhaps already taken a look at the use of the NamedTemporaryFile?
An example of how to use this in the context of GeoLib is described in the documentation here: Software integrations - GEOLIB | VIKTOR Documentation

Let me know if that works!

Sorted now thanks Marcel :+1: I am using the python-ags4 package which did allow a buffer.

Also thank you Daniel I had not known about NamedTemporaryFiles, I will keep it in mind now if i come across that sort of issue.

1 Like