Sample of app with file upload?

Hi there,

I was wondering if someone could show a code snippet of app functionality to upload a file. I want the user to be able to select a file to upload but I could not get find a way to do this when reading the CsvFile section.

Thanks in advance,
Rob

Hi,

Welcome to the community forum!

You can find information about creating file uploads here: Topics - Files | VIKTOR Documentation

Regards,

Raoul

Hi Rob,

Welcome! As noted above, you can find examples here and here.

Can you elaborate on why you didn’t succeed implementing it based on reading the CsvFile example? Maybe we can improve upon the documentation.

Hi Bas,

First of all it’s been a while since I got a Viktor training (1.5 years) and I haven’t written any code for this platform since but this is how I expected it to work (coming from flutter / python web apps logic).

I need a view with a button and clicking that button should be coupled to an event in my controller. So my code now is like;

class CalculationParametrization(Parametrization):
    ...
    uploads = Tab('Uploads')
    uploads.btn_upload = ActionButton("Analysis", "process_file", longpoll=False)

class CalculationController(ViktorController):
    ...
    parametrization = CalculationParametrization
    ....
    @ParamsFromFile()
    def process_file(self, file: File, **kwargs):  # viktor.core.File
        return {}

but this keeps giving me the ValueError: Entity does not have a file.' error

The upload of a file is not coupled to a (upload) button, but to an entity type. So, you can remove the Parametrization part and keep the Controller part. The @ParamsFromFile decorator is enough to ‘trigger’ the entity type to be a file-type. In the app in the browser you will get to see a file-upload screen if hitting the + for adding an entity of that type (so uploading of a file does not occur within the editor of the entity, but actually at adding the entity).

Uploading file(s) within an editor using a button is not possible, but is a feature request on our radar: File Upload Field

Ok, got it… but I definitely support the feature request.

Thanks!