我正在寫燒瓶web應用程序,我們可以在其中上傳圖片並保存在文件系統中。我還希望保存包含城市,拍攝日期等信息的圖像,幷包含更多功能,例如按照日期排序圖像,在用戶中受歡迎,按城市排序。 如果我將圖像保存到文件系統而不是數據庫,該怎麼辦?上傳並保存圖片信息
import os
from flask import Flask, render_template, request, redirect, url_for,
send_from_directory
from werkzeug.utils import secure_filename
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static/uploads')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['ALLOWED_EXTENSIONS'] = set(['png', 'jpg', 'jpeg','gif'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',filename=filename))
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],filename)
if __name__ == '__main__':
app.run(port=4555)
後的代碼作爲文本,而不是圖片能夠複製和粘貼 – Micho