Saving Upload in Flask Only Saves to Project Root

Saving upload in Flask only saves to project root

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

Join the secured filename to the upload folder, then save to that path.

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

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

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 the user running the application has write permission to that directory. If you get permission errors when running with mod_wsgi, for example, the user is commonly httpd or www-data. If you get a permission denied error, check that.

Why is my flask route saving xlsx file to the root directory of the project instead of instance files?

You are saving the file in root directory in generate_prev_sim_csv function

filename = "Simulation_Summary.xlsx"
[...]
wb.save(filename=filename)

Wb.save creates a file if it doesn't exist so you don't need to create file in your route

Just change the filename to this in your openpyxl function

filename = 'instance/files/Simulation_Summary.xlsx'

Flask: IOError when saving uploaded files

The slash at the beginning of '/uploads' makes the path specification absolute: the leading slash represents the root of the filesystem hierarchy. While that might not be exactly how things work on Windows, it makes sense for Python to understand it this way as its path-handling functions are cross-platform.

The forms 'uploads/' and './uploads/' are equivalent and they are relative.

Note that relative paths are relative to the current directory, which you don't necessarily control, so you might want to specify an absolute path for UPLOAD_FOLDER.

how to upload the images inside a folder 'images' instead of the curent directory in flask

Your code has app.config['UPLOADED_PHOTOS_DEST'] but doesn't make use of it, so i would suggest doing that.

I would make a directory called images in your root application directory, and then follow either of these approaches:

1) Using config

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(APP_ROOT, 'images')

@app.route('/', methods=['GET', 'POST'])
def upload_file():
target = app.config['UPLOADED_PHOTOS_DEST']

2) Without using config

@app.route('/', methods=['GET', 'POST'])
def upload_file():
target = os.path.join(APP_ROOT, 'images')

Other considerations:

  • You can specify the filepath for UPLOADED_PHOTOS_DEST in a separate config file

  • In your current code, target is unused, so you will need to pass in that filepath to your image processing



Related Topics



Leave a reply



Submit