def getself # 获取图片目录路径 image_dir = current_appconfigDOWNLOAD_DIR responses = list # 遍历目录下的所有文件 for filename in oslistdirimage_dir # 构建文件路径 imag
根据报错信息,可以看出请求的URL没有找到,可能是文件路径的问题。send_from_directory函数需要指定文件所在的目录路径,然后再指定要下载的文件名。所以,可以尝试使用send_file函数来发送文件给客户端进行下载。send_file函数需要指定完整的文件路径。
以下是修改后的代码示例:
from flask import send_file, current_app
import os
def get(self):
# 获取图片目录路径
image_dir = current_app.config['DOWNLOAD_DIR']
responses = list()
# 遍历目录下的所有文件
for filename in os.listdir(image_dir):
# 构建文件路径
image_path_ = os.path.join(image_dir, filename)
image_path = os.path.abspath(image_path_)
# 发送文件给客户端进行下载
response = None
try:
response = send_file(image_path, as_attachment=True)
responses.append(response)
# 删除图片文件
# os.remove(image_path)
except Exception as e:
print("Error sending file:", str(e))
responses.append(response)
return responses
请注意,这段代码会将目录下的所有文件都发送给客户端进行下载,如果只想下载指定文件,可以修改代码逻辑
原文地址: https://www.cveoy.top/t/topic/ieeq 著作权归作者所有。请勿转载和采集!