seosnaps commited on
Commit
2042b80
·
verified ·
1 Parent(s): eddfe24

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, send_from_directory
2
+ import os
3
+
4
+ app = Flask(__name__)
5
+
6
+ UPLOAD_FOLDER = 'uploads'
7
+ if not os.path.exists(UPLOAD_FOLDER):
8
+ os.makedirs(UPLOAD_FOLDER)
9
+
10
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
11
+
12
+ @app.route('/', methods=['GET', 'POST'])
13
+ def upload_file():
14
+ if request.method == 'POST':
15
+ if 'file' not in request.files:
16
+ return 'No file part'
17
+
18
+ file = request.files['file']
19
+
20
+ if file.filename == '':
21
+ return 'No selected file'
22
+
23
+ if file:
24
+ filename = file.filename
25
+ file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
26
+ return 'File uploaded successfully'
27
+
28
+ return '''
29
+ <!doctype html>
30
+ <title>Upload new File</title>
31
+ <h1>Upload new File</h1>
32
+ <form method=post enctype=multipart/form-data>
33
+ <input type=file name=file>
34
+ <input type=submit value=Upload>
35
+ </form>
36
+ '''
37
+
38
+ @app.route('/files')
39
+ def list_files():
40
+ files = os.listdir(app.config['UPLOAD_FOLDER'])
41
+ return '''
42
+ <!doctype html>
43
+ <title>Uploaded files</title>
44
+ <h1>Uploaded files</h1>
45
+ <ul>
46
+ ''' + ''.join(['<li><a href="/download/{}">{}</a></li>'.format(f, f) for f in files]) + '''
47
+ </ul>
48
+ '''
49
+
50
+ @app.route('/download/<filename>')
51
+ def download_file(filename):
52
+ return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
53
+
54
+ if __name__ == '__main__':
55
+ app.run(debug=True, port=7860)