python 多部分帖子仅适用于 pyinstaller。与 vscode 运行 python 文件完美配合

问题描述 投票:0回答:1
from tkinter import filedialog

self.file = filedialog.askopenfile(
            mode="r", filetypes=[("Excel files", ".xlsx .xls")]
        )

当我将上面的文件传递到 POST 时,如下所示。

它正在与 vscode“运行 Python 文件”一起使用

但是用pyinstaller编译成exe后。

POST 成功并返回状态 200,但未上传。

我不确定这是一种绝对路径问题。

    field_data = {
        "data": (None, json.dumps(data), "application/json"),
        "file": (
            os.path.basename(file.name),
            open(os.path.abspath(file.name), "rb"),
            "multipart/form-data",
        ),
    }
    m = MultipartEncoder(fields=field_data)
    headers = {"Content-Type": m.content_type}

    API_ENDPOINT = (
        "https://uploadfile"
    )

    session = requests.Session()
    session.trust_env = False
    r = session.post(url=API_ENDPOINT, data=m, headers=headers)
python tkinter http-post multipartform-data
1个回答
0
投票

试试这个:

r = session.post(url=API_ENDPOINT, data=json.dumps(data), files={'file': open(os.path.abspath(file.name), "rb")}, headers=headers)
© www.soinside.com 2019 - 2024. All rights reserved.