ckeditor 圖片上傳

筆記種類
ckeditor

 

in __init__.py

    ckeditor.init_app(app)
    app.config['CKEDITOR_FILE_UPLOADER'] = 'pages.upload'
    app.config['CKEDITOR_PKG_TYPE'] = 'standard'

    #接著在routes裡創一個處理圖片的function

in routes.py

   pages = Blueprint(
        "pages" ,
        __name__ ,
        template_folder="templates",
        static_folder="static"
    )



@pages.route('/files/<path:filename>')
def uploaded_files(filename: str):
    path = 'update/update-pic'
    return send_from_directory(path, filename)


@pages.route('/upload', methods=['POST'])
def upload():
    f = request.files.get('upload')
    # Add more validations here
    extension = f.filename.split('.')[-1].lower()
    if extension not in ['jpg', 'gif', 'png', 'jpeg','svg']:
        return upload_fail(message='Image only!')
    #進行檔案格式的篩選
    now = datetime.now()
    date_str = now.strftime("%Y-%m-%d")
    filename = f"{date_str}.{secure_filename(f.filename)}"  # 增加日期到檔名前
    f.save(os.path.join('update/update-pic', filename))     # 創一個資料夾update/update-pic
    url = url_for('pages.uploaded_files', filename=filename)
    return upload_success(url, filename=filename)  # return upload_success call





<script>
		// Enable local "codesnippet" plugin from /ckeditor/static/standard/plugins/codesnippet/ path.
		CKEDITOR.plugins.addExternal( 'codesnippet', '/ckeditor/static/standard/plugins/codesnippet/', 'plugin.js' );
	</script>

 

Visit the link for more information:
References