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来说还很陌生,因此我已经坚持了一段时间,希望能对您有所帮助。预先感谢
答案 0 :(得分:1)
uploaded_file
不是文件名,它是Flask FileStorage
对象。您可以直接从中读取内容,无需致电open()
。
那就这样做吧
empPicture = uploaded_file.read()
答案 1 :(得分:0)
在调用“ convertToBinaryData ”时,您传递的不是文件名而是对象的“ uploaded_file ”。
您需要将文件名(如果保存在自定义位置,请使用正确的路径)传递给您的“ convertToBinaryData ”功能。
类似这样的东西:
convertToBinaryData(uploaded_file.filename)