Uploading a file using File object

We have a requirement to upload a file using browser library’s keyword ‘Promise to Upload file’. However, We do not have a physical file to provide the path, Instead we have a file object with us. How do we achieve upload in this case? Could someone assist on this?

Hi,

If your file object/content can be written, you could create a temporary file to upload, then manage to remove it at the end of file?
Something like this:

import tempfile

def create_temp_file(content):
    with tempfile.NamedTemporaryFile(delete=False, mode='w') as temp_file:
        temp_file.write(content)
        return temp_file.name

Other than that and a physical file, I’m not sure you can pass a file-object.
Hopefully someone has a solution.

Regards
Charlie

1 Like

I’ll second what charlie said, but add that OperatingSystem library has Create File and Remove File to help you do this, combine that with Built-in variable’s ${TEMPDIR} variable and you should have everything you need to create a temporary file and then remove it within your test.

Dave.

1 Like