jeudi 26 avril 2018

Looping over large files received from web and saving them - flask

I'm new with flask, and web in general. I need to upload few large files using flask and save them. My script is the following:

UPLOAD_FOLDER = '/home/billy/genesort/dataset_for_chipseq'
ALLOWED_EXTENSIONS = set(['txt', 'txt.gz'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = "super secret key"


def allowed_file(filename):   
    if '.gz' in filename:
        return '.' in filename and \
          filename.rsplit('.', 2)[1] in ALLOWED_EXTENSIONS
    else:
        return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


@app.route('/run-pipeline', methods=['POST'])
def run_script():
    treat_files = request.files.getlist('treat_files')
    for my_file in treat_files:

       print(my_file)

       file = request.files[my_file]

       print(file)

       if file and allowed_file(file.filename):
           filename = secure_filename(file.filename)
           file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
           return 'Uploaded successfully'

       return 'Could not upload file'

if __name__ == '__main__':
    app.run(debug=True)

I'm using POSTMAN to demo the web part.

I think the upload is good, since it prints my_file OK, but the second print statement, is not executed, and the following saving part failed. in POSTMAN I got the error: The browser (or proxy) sent a request that this server could not understand Anyone knows how to fix it?




Aucun commentaire:

Enregistrer un commentaire