Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
Saving upload in Flask only saves to project root

When I upload a new file, it saves to the application root folder, even though I specified a different UPLOAD_FOLDER. Why doesn't the config work?

views.py:

from flask import render_template
from flask import request, redirect, url_for,flash
from werkzeug.utils import secure_filename
from app import app
import os

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLD = '/Users/blabla/Desktop/kenetelli/htmlfi'
UPLOAD_FOLDER = os.path.join(APP_ROOT, UPLOAD_FOLD)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/')
def tmrf():
    return render_template('main.html')


@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        f.save(secure_filename(f.filename))
    return 'file uploaded successfully'

__init__.py:

from flask import Flask

UPLOAD_FOLDER = ''
ALLOWED_EXTENSIONS = set('*.doc')

app = Flask(__name__)
app.config.from_object('config')
from app import views
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

UPLOAD_FOLDER is not a configuration option recognized by Flask. f.save works relative to the current working directory, which is usually the root of the project during development.

Attach the protected filename to the upload folder and then save it to that path.

 f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))archivo protegido

It is better to store local data in the instance folder , not in the root of the project. Flask already knows where that is. Just make sure you create the instance directory first.

 instance import os from werkzeug.utils import secure_filename # create the folders when setting up your app os.makedirs(os.path.join(app.instance_path, 'htmlfi'), exist_ok=True) # when saving the file f.save(os.path.join(app.instance_path, 'htmlfi', secure_filename(f.filename)))

No matter where you decide to save it, you need to make sure that the user running the app has write permission to that directory. If you get permission errors when you run mod_wsgi, for example, the user is usually httpd or www-data . If you get a permission denied error, check it out.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda