通过python代码发送文件但不通过POSTMAN发送文件时出错

问题描述 投票:0回答:1

我尝试通过 Streamlit 应用程序上传文件,但出现以下错误:将文件读取为目录

要上传的 Streamlit 代码

    if uploaded_file is not None:
        fname = uploaded_file.name
        img = Image.open(uploaded_file) # Open image in PIL
        col1.image(img)     #Display the Imgage on ST
        img.save(b, 'jpeg') #Send image to the io module
        img = b.getvalue()  #Convert to bytes
        res = self.request(img)

调用API的代码:

class response():

  def crackD(f):

    body = {'deviceSize':('', '(0.0, 0.0, 375.0, 667.0)') ,'ConsumerID':('', '-100'), 'client':'crackd-api', 'metadata':('', '{"source": "streamlit_test"}'), 'file': f} #Body of the post    
    headers = {"Postman-Token":"xxx","cache-control":"no-cache","x-access-token":"xxx" }  
    response = requests.post(xxx, headers = headers, files=body)         
    return response

       

但是通过POSTMAN成功调用api..

python streamlit
1个回答
0
投票

从您提供的错误信息来看,原因是上传的内容是一个文件夹,而不是一个简单的文件。

您可以参考官方文件上传示例来排查您的问题。

https://docs.streamlit.io/library/api-reference/widgets/st.file_uploader

© www.soinside.com 2019 - 2024. All rights reserved.