【发布时间】:2023-03-11 05:51:01
【问题描述】:
我试图删除我在网络服务上显示后创建的文件。 但是,对于该函数的当前位置,它显示“未访问 remove_file(response)”,当我运行 web 服务时,输出文件时出现错误,因为它显然已被删除。
我不知道该怎么办了,我尝试将 afer_this_request 函数放在 upload_file 声明之后,在返回渲染模板之后,所有这些都不起作用。我快疯了,所以任何帮助都会让我度过愉快的一年!
@app.route('/', methods=['POST', 'GET'])
def upload_file():
if request.method == 'POST':
myuuid = uuid.uuid4()
# check if the post request has the file part
if 'file' not in request.files:
return render_template('error.html')
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
return render_template('error.html')
if file and allowed_file(file.filename):
sfilename = secure_filename(file.filename)
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.mkdir(app.config['UPLOAD_FOLDER'])
output_path = os.path.join(app.config['UPLOAD_FOLDER'], sfilename)
file.save(output_path)
if 'Alpha Miner' in request.form:
Alpha(pro_file, myuuid)
img_url = url_for('static', filename=str(myuuid) + '.gv.svg')
@after_this_request
def remove_file(response):
os.remove(img_url)
return response
titel = "Petri Net for Alpha Miner: " + sfilename
return render_template('upload.html', img_url=img_url, titel = title)
【问题讨论】:
-
不是吗?我用烧瓶导入它:flask.palletsprojects.com/en/2.0.x/api/…