4

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))
0

2 Answers 2

8

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))
Sign up to request clarification or add additional context in comments.

2 Comments

Uh, it was easy :) Thank you!
what was the app.config for?
0

You can try the code below. It worked perfectly for me.

base_path = os.path.abspath(os.path.dirname(__file__))
upload_path = os.path.join(base_path, app.config['UPLOAD_FOLDER'])
f.save(os.path.join(upload_path, secure_filename(f.filename)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.