Spaces:
seosnaps
/
Runtime error

01 / app.py
seosnaps's picture
Update app.py
ad07929 verified
raw
history blame
1.6 kB
from flask import Flask, request, send_from_directory,abort
import os
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/', methods=['GET', 'POST'])
def home():
abort(500, "Server Error")
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return 'No file part'
file = request.files['file']
if file.filename == '':
return 'No selected file'
if file:
filename = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return 'File uploaded successfully'
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
@app.route('/files')
def list_files():
files = os.listdir(app.config['UPLOAD_FOLDER'])
return '''
<!doctype html>
<title>Uploaded files</title>
<h1>Uploaded files</h1>
<ul>
''' + ''.join(['<li><a href="/download/{}">{}</a></li>'.format(f, f) for f in files]) + '''
</ul>
'''
@app.route('/download/<filename>')
def download_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True)