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)
试试这个:
r = session.post(url=API_ENDPOINT, data=json.dumps(data), files={'file': open(os.path.abspath(file.name), "rb")}, headers=headers)