首页 > 解决方案 > TypeError: expected str, bytes or os.PathLike object, not FileStorage 不断弹出

问题描述

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

这是我将图像转换为二进制的功能...

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

这是接收和保存上传文件的代码块,但是,当它运行时,我收到此错误...

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

我对python很陌生,我已经坚持了一段时间,任何帮助将不胜感激。提前致谢

标签: pythonflaskfile-upload

解决方案


在调用“ convertToBinaryData ”时,您传递的是“ upload_file ”,它不是文件名而是对象。

您需要将文件名(如果保存在自定义位置,则使用正确的路径)传递给您的“ convertToBinaryData ”功能。

像这样的东西:

convertToBinaryData(uploaded_file.filename)

推荐阅读