我正在尝试通过将代码分成方法 — get 和 post 来改进我的代码。
有效的代码:
@app.route('/send', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file:
// do sth//
return 'result'
return '''
<!doctype html>
<title>Upload new Filess</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
但是当我尝试将其更改为类并分为 get 和 post 方法时,它显示字符串而不是 html 形式。
更新代码:
class Form(Resource):
def get(self):
return '''
<!doctype html>
<title>Upload new Filess</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
def post(self):
file = request.files['file']
if file:
// do sth//
return 'result'
api.add_resource(Form, '/send')
我看不到问题。 我尝试过更改表格,但问题似乎与表格无关。
您应该添加一个 内容类型标头[stackoverflow] 告诉浏览器正在经历什么,以便它知道如何处理。