12345678910111213141516171819202122232425262728293031323334353637383940 |
- import os
- from flask import Flask, send_from_directory, jsonify, current_app
- from flask_cors import CORS, cross_origin
- from werkzeug.utils import secure_filename
-
-
- app = Flask(__name__, static_url_path='')
- app.config["SECRET_KEY"] = "f82abccd93a28b6cdda4525c9afa7a30"
- app.config["CORS_HEADERS"] = "Content-Type"
- app.config['UPLOAD_FOLDER'] = './articles'
-
- cors = CORS(app, resources={r"/articles": {"origins": "http://localhost:5500"}})
-
-
- @app.route("/articles/<path:article>")
- @cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
- def send_text_file(article):
- return send_from_directory(directory="articles", path=article)
-
- @app.route("/articles")
- @cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
- def list_files():
- files = list()
- for filename in os.listdir(os.path.join(current_app.root_path, "articles")):
- path = os.path.join(current_app.root_path, "articles", filename)
- if os.path.isfile(path):
- files.append(filename)
- return jsonify(files)
-
- @app.route("/" ,methods = ['POST'])
- def upload_file():
- if requested.method == 'POST':
- file=request.files['file']
- if file:
- filename = secure_filename(file.filename)
- file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
- return list_files()
-
- if __name__ == "__main__":
- app.run()
|