# -*- coding:gb2312 -*-
#!/usr/bin/python
# Filename: upload_file.py
from flask import request
from flask import Flask
from flask import render_template
from werkzeug.utils import secure_filename
app = Flask(__name__)
@app.route('/')
def index():
    return 'Index Page'
@app.route('/hello')
def hello():
    return 'Hello~World~'
@app.route('/user/<username>')
def show_user_profile(username):
    # show the user profile for that user
    return 'User %s' % username
@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id
# ##############################################
@app.route('/upload', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        return do_file_upload(request)
    else:
        return show_file_upload_form()
def show_file_upload_form():
    return render_template('file_upload_form.html')
def do_file_upload(request):
    if request.method == 'POST':
        f = request.files['file']
        f.save(secure_filename(f.filename))
    return render_template('file_upload_finish.html')
if __name__ == '__main__':
    app.run(debug=True)
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> templates/file_upload_form.html </title>
  <meta charset="UTF-8">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>
 <body>
<form method="post" action="upload" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit">    
</form>
 </body>
</html>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> templates/file_upload_finish.html </title>
  <meta charset="UTF-8">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>
 <body>
    <h1>file_upload_finish</h1>
 </body>
</html>