Created
August 10, 2018 11:50
-
-
Save Phoenix124/0f8c8b06c11a624c531f84c2a23e9c11 to your computer and use it in GitHub Desktop.
Upload file Flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from flask import Flask, request, redirect, url_for, send_from_directory | |
from flask_cors import CORS | |
from werkzeug.utils import secure_filename | |
UPLOAD_FOLDER = 'files' | |
ALLOWED_EXTENSIONS = {'pcap'} | |
app = Flask(__name__) | |
CORS(app) | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
def allowed_file(filename): | |
return '.' in filename and \ | |
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS | |
@app.route('/upload', methods=['POST']) | |
def upload_file(): | |
print(request.files) | |
# check if the post request has the file part | |
if 'file' not in request.files: | |
print('no file in request') | |
return"" | |
file = request.files['file'] | |
if file.filename == '': | |
print('no selected file') | |
return"" | |
if file and allowed_file(file.filename): | |
print("hello") | |
filename = secure_filename(file.filename) | |
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
return "" | |
print("end") | |
return"" | |
@app.route('/upload/<filename>', methods=['GET']) | |
def uploaded_file(filename): | |
return send_from_directory(app.config['UPLOAD_FOLDER'], | |
filename) | |
if __name__ == "__main__": | |
app.run(h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment