Last active
August 29, 2015 14:22
-
-
Save elvisw/61c76b6c76b4c5cb905a to your computer and use it in GitHub Desktop.
Flask-Store 扩展实现上传
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Flask-WTF | |
Flask-Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>上传文件</title> | |
</head> | |
<body> | |
<form method="POST" action="{{ url_for('upload') }}" enctype="multipart/form-data"> | |
{{ form.hidden_tag() }} | |
{{ form.file.label }} {{ form.file(size=20) }} | |
<input type="submit" value="上传"> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from flask import Flask, flash, render_template, request | |
from flask.ext.store import Store | |
from flask_wtf import Form | |
from wtforms import FileField | |
import os | |
basedir = os.path.abspath(os.path.dirname(__file__)) | |
app = Flask(__name__) | |
app.config['STORE_DOMAIN'] = 'http://127.0.0.1:5000' | |
app.config['STORE_PATH'] = os.path.join(basedir, 'uploadfile') | |
app.config['SECRET_KEY'] = 'kP/dWZhSd5Dqn8IYUAo+EpoDL2PcHina' | |
store = Store(app) | |
class UploadForm(Form): | |
file =FileField(u'上传文件') | |
@app.route('/upload/', methods=('GET', 'POST')) | |
def upload(): | |
form = UploadForm() | |
if form.validate_on_submit(): | |
provider = store.Provider(request.files.get('file')) | |
provider.save() | |
flash(u'已完成上传', 'success') | |
return provider.absolute_url | |
return render_template('upload.html', form=form) | |
if __name__ == "__main__": | |
app.run(debug=True, threaded=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment