html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <p> <input type="file" name="file"> <input type =submit value="upload"> </p> </form> </body> </html>
views处理函数视图文件: from flask import Flask, request,make_response,render_template, redirect, url_for from werkzeug.utils import secure_filename # 使用这个是为了确保filename是安全的 from os import path from flask import Blueprint, render_template blue = Blueprint("blue", __name__) @blue.route("/upload/",methods=['GET','POST']) def upload(): if request.method=='POST': #通过request.files f = request.files["file"] #<FileStorage: '4.jpg' ('image/jpeg')> print(f) #获取你的应用的路径 base_path = path.abspath(path.dirname(__file__)) # /home/guojie/wenjian/myapp print(base_path) #拼接你上传的图片路径 upload_path = path.join(base_path,'static/uploads/') #/home/guojie/wenjian/myapp/static/uploads/ print(upload_path) #secure_filename() 对上传的文件名进行安检 file_name = upload_path + secure_filename(f.filename) print(file_name) #进行保存 f.save(file_name) print(3) return redirect(url_for('blue.upload')) return render_template('upload.html')
|