#Sample.pyhtml
# coding:utf-8 from flask import Flask,render_template,request,redirect,url_for from werkzeug.utils import secure_filename import os app = Flask(__name__) @app.route('/upload', methods=['POST', 'GET']) def upload(): if request.method == 'POST': f = request.files['file'] basepath = os.path.dirname(__file__) # 當前文件所在路徑 upload_path = os.path.join(basepath, 'static\uploads',secure_filename(f.filename)) #注意:沒有的文件夾必定要先建立,否則會提示沒有該路徑 f.save(upload_path) return redirect(url_for('upload')) return render_template('upload.html') if __name__ == '__main__': app.run(debug=True)
#upload.htmlflask
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>文件上傳示例</h1> <form action="" enctype='multipart/form-data' method='POST'> <input type="file" name="file"> <input type="submit" value="上傳"> </form> </body> </html>
這裏要注意:<form>標籤裏的enctype屬性必定要填寫'multipart/form-data'app
意思是不加密,上傳文件的時候必定要選這個,否則不行加密
好了接下來咱們看看運行效果url
1. 初始界面spa
2. 選擇一個文件,點擊上傳debug
3. 最後網頁會回到初始界面,而後上傳的文件,也保存在咱們指定的目錄上了code
至此,項目結束orm