-
-
Save born2discover/135562a128231f50dd925d89c7761016 to your computer and use it in GitHub Desktop.
flask-uploads-sample
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from flask import * | |
import os | |
from werkzeug import secure_filename | |
app = Flask(__name__) | |
#searchword = request.args.get('q', '') | |
UPLOAD_FOLDER = '/code/flask-test/upload_files' | |
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
@app.route("/") | |
def hello(name=None): | |
return render_template('hello.html', name=name) | |
def allowed_file(filename): | |
return '.' in filename and \ | |
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS | |
@app.route('/file', methods=['GET', 'POST']) | |
def upload_file(): | |
if request.method == 'POST': | |
file = request.files['file'] | |
if file and allowed_file(file.filename): | |
# filename = secure_filename(file.filename) | |
filename = file.filename | |
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
return redirect(url_for('upload_file')) | |
else: | |
return ''' | |
<!doctype html> | |
<title>Upload new File</title> | |
<h1>File Select Error!</h1> | |
<a href="/file">file</a> | |
''' | |
return ''' | |
<!doctype html> | |
<title>Upload new File</title> | |
<h1>Upload new File</h1> | |
<form action="" method=post enctype=multipart/form-data> | |
<p><input type=file name=file> | |
<input type=submit value=Upload> | |
</form> | |
''' | |
@app.route("/user/<username>") | |
def profile(username): | |
return u"こんにちは!"+username+u"くん!" | |
if __name__ == "__main__": | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment