Path not valid from File Field

(VIKTOR SDK 14.15 python 3.11)

Hi, I have an issue with ImageView: I simply want a user to upload an image via a FileField that is then shown in an ImageView tab.

This is my code in the Controller: (in Parametrization the FileField has attribute name = P_general_uploadimage)

    @ImageView('Afbeelding', duration_guess=2)
    def voorbeeldplaatje(self, params, **kwargs):
        file_path = Path(__file__).parent / params.P_general_uploadimage.filename
        return ImageResult.from_path(file_path)

Uploading a file that is located in the design folder works fine, but uploading a file from a generic location gives the following error:


ValueError: path '/home/user/src/app/design/image.png' is not a valid file-path.

How should I correctly specify the path to the uploaded file?

Hi,

The FileField is available in your params as a FileResource object. The FileResource itself has a file property that returns the File object.

You can use it as follows:

@ImageView('Afbeelding', duration_guess=2)
def voorbeeldplaatje(self, params, **kwargs):
    file = params.P_general_uploadimage.file
    return ImageResult(image=file)

More information on using a file upload can be found in the docs

Regards,

Raoul