文件保存未以正确的格式保存文件



我必须在两个地方上传文件,

  1. 在本地目录中
  2. 在吉拉通过卷曲

我已经编写了post端点,该端点从请求中读取文件并通过请求将同一文件发送到 Jira,并在成功响应后将其保存在本地。

我的代码如下所示

for file in request.files.getlist('file'):
filename = file.filename
mimetype = file.content_type
if not is_valid_type(mimetype):
return json.dumps({"success": False, "message": "Invalid File Format" }), 415
files = {'file': (filename, file, mimetype)}
r = requests.post(jira_url, files=files, headers=headers, auth=HTTPBasicAuth(current_app.config.get('username'), current_app.config.get('password')),verify=False)
LOG.info("Got %s response from %s - text %s", r.status_code, "upload", r.json())
data = r.json()
filename = secure_filename(file.filename)
file.save(os.path.join(current_app.config["UPLOAD_FOLDER"], filename))

它保存了文件,但是当我尝试打开它时,它说我们不支持这种文件格式。

如果我从循环中删除对 Jira 的post调用,那么它会以正确的格式保存文件。

你试过打开/写吗?

with open("input_file.txt", "w") as text_file:
text_file.write("destination_file")
text_file.close()

指定路径目标的解决方案:

import os
filename = "input_file.txt"
path = "/pathlocation/..."
fullpath = os.path.join(path, filename)
with open("input_file.txt", "w") as text_file:
text_file.write(fullpath)
text_file.close()

最新更新