提交 e95a34d4 编写于 作者: H hjdhnx

修复后台上传文件后崩溃的异常

上级 bd9b5834
...@@ -169,28 +169,31 @@ def upload_file(): ...@@ -169,28 +169,31 @@ def upload_file():
return render_template('login.html') return render_template('login.html')
if request.method == 'POST': if request.method == 'POST':
try: try:
f = request.files['file'] file = request.files['file']
# print(f.size) # print(f.size)
# print(f) # print(f)
# print(request.files) # print(request.files)
filename = secure_filename(f.filename) filename = secure_filename(file.filename)
print(f'推荐安全文件命名:{filename}') print(f'推荐安全文件命名:{filename}')
# savePath = f'js/{filename}' # savePath = f'js/{filename}'
savePath = f'js/{f.filename}' savePath = f'js/{file.filename}'
# print(savePath) # print(savePath)
if os.path.exists(savePath): if os.path.exists(savePath):
return jsonify(error.failed(f'上传失败,文件已存在,请先查看删除再试')) return jsonify(error.failed(f'上传失败,文件已存在,请先查看删除再试'))
with open('js/模板.js', encoding='utf-8') as f2: with open('js/模板.js', encoding='utf-8') as f2:
before = f2.read() before = f2.read()
upcode = f.stream.read().decode('utf-8') upcode = file.stream.read().decode('utf-8')
check_to_run = before + upcode check_to_run = before + upcode
# print(check_to_run) # print(check_to_run)
try: try:
js2py.eval_js(check_to_run) js2py.eval_js(check_to_run)
except: except:
return jsonify(error.failed('文件上传失败,检测到上传的文件不是drpy框架支持的源代码')) return jsonify(error.failed('文件上传失败,检测到上传的文件不是drpy框架支持的源代码'))
print(savePath)
# savePath = os.path.join('', savePath)
# print(savePath) # print(savePath)
f.save(savePath) file.seek(0) # 读取后变成空文件,重新赋能
file.save(savePath)
return jsonify(error.success('文件上传成功')) return jsonify(error.success('文件上传成功'))
except Exception as e: except Exception as e:
return jsonify(error.failed(f'文件上传失败!{e}')) return jsonify(error.failed(f'文件上传失败!{e}'))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册