TypeError: expected str, bytes or os.PathLike object, not FileStorage keeps popping up

use*_*123 5 python file-upload flask

def convertToBinaryData(filename):
    # Convert digital data to binary format
    with open(filename, 'rb') as file:
        binaryData = file.read()
    return binaryData
Run Code Online (Sandbox Code Playgroud)

This is my function for converting an image to binary...

uploaded_file = request.files['file']
if uploaded_file.filename != '':
    uploaded_file.save(uploaded_file.filename)
    empPicture = convertToBinaryData(uploaded_file)

Run Code Online (Sandbox Code Playgroud)

and this is the block of code where the uploaded file is received and saved, however, when it runs, I get this error...

    with open(filename, 'rb') as file:
TypeError: expected str, bytes or os.PathLike object, not FileStorage
Run Code Online (Sandbox Code Playgroud)

I'm pretty new to python and I've been stuck on this for a while, any help would be appreciated. Thanks in advance

小智 4

在调用“ convertToBinaryData ”时,您正在传递“ uploaded_file”' which is not a filename but and object.

您需要将文件名(如果保存在自定义位置,则具有正确的路径)传递到您的“ convertToBinaryData” ”函数。

像这样的东西:

convertToBinaryData(uploaded_file.filename)
Run Code Online (Sandbox Code Playgroud)