Spaces:
Runtime error
Runtime error
File size: 1,601 Bytes
ad07929 a7c6564 ad07929 a7c6564 ad07929 a7c6564 ff7d7e0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
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)
|