Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change upload path (UPLOAD_FOLDER) during the runtime

Tags:

python

flask

How to change the upload folder during the runtime? I'd like to be able to change the location where file will be uploaded to, but I don't know how.

I tried something like this, but I get KeyError:

@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    path = 'uploads/text'
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        UPLOAD_FOLDER = path
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return redirect(url_for('uploaded_file',
                                filename=filename))
like image 365
user3448282 Avatar asked Oct 31 '25 19:10

user3448282


1 Answers

You can try this

file.save(os.path.join(/path/to/save/, filename))

In yours

UPLOAD_FOLDER = path
file.save(os.path.join(UPLOAD_FOLDER, filename))
like image 119
itzMEonTV Avatar answered Nov 02 '25 10:11

itzMEonTV