Skip to content

Commit

Permalink
examples: update server.py (#4732)
Browse files Browse the repository at this point in the history
  • Loading branch information
codehero7386 authored Oct 16, 2023
1 parent 81b0999 commit 8115538
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions examples/python-xhr/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,33 @@
from flask_cors import CORS

UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.path.join(os.path.dirname(__file__), UPLOAD_FOLDER)
CORS(app)

def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/upload', methods=['POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
print (request.files)
if len(request.files) == 0:
return jsonify(
error="No file n request"
), 400
for fi in request.files:
file = request.files[fi]
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return jsonify(
message="ok"
), 201
uploaded_files = request.files.getlist('file')
if not uploaded_files:
return jsonify(error="No files in the request"), 400

uploaded_filenames = []
for file in uploaded_files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
uploaded_filenames.append(filename)

if uploaded_filenames:
return jsonify(message="Files uploaded successfully", uploaded_files=uploaded_filenames), 201
else:
return jsonify(error="No valid files uploaded"), 400

if __name__ == '__main__':
app.run(port=3020)
app.run(port=3020)

0 comments on commit 8115538

Please sign in to comment.