feat: add upload and download function
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
from flask import Flask, request, send_from_directory, abort
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
UPLOAD_FOLDER = './uploads'
|
||||
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
||||
|
||||
@app.route('/upload', methods=['POST'])
|
||||
def upload_file():
|
||||
if 'file' not in request.files:
|
||||
return 'No file part'
|
||||
|
||||
file = request.files['file']
|
||||
if file.filename == '':
|
||||
return 'No selected file'
|
||||
|
||||
# 保存文件到指定目录
|
||||
file.save(os.path.join(UPLOAD_FOLDER, file.filename))
|
||||
return 'File uploaded successfully'
|
||||
|
||||
@app.route('/download/<filename>', methods=['GET'])
|
||||
def download_file(filename):
|
||||
try:
|
||||
return send_from_directory(UPLOAD_FOLDER, filename, as_attachment=True)
|
||||
except FileNotFoundError:
|
||||
abort(404)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=880)
|
||||
Reference in New Issue
Block a user