Spaces:
Runtime error
Runtime error
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 | |
def home(): | |
abort(500, "Server Error") | |
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> | |
''' | |
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> | |
''' | |
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) | |