0

I want to store any uploaded image to the static/customlogos folder with the name "logo.png" no matter what its actual name is. I have a basic Flask setup with the typical static and template folders. For simplicity I removed things like extension validation in my code below. However doing it like this throws a FileNotFound error. Since I want to run my app on various environments, I don't want to use a static path. What am I doing wrong? Thanks for your help.

latestfile = request.files['customlogo']
#This prints the file name of the uploaded file
print(latestfile.filename)
#I want to save the uploaded file as logo.png. No matter what the uploaded file name was.
latestfile.save(os.path.join('/static/customlogos', 'logo.png'))
jz22
  • 2,328
  • 5
  • 31
  • 50
  • The path `/static/customlogos` refers to a directory on your file system. In this case, the path this code tries to save the file under is `/static/customlogos/logo.png` – Kendas Oct 17 '17 at 14:18
  • have you create the `/static/customlogos/` ?, i have same error like that, my problem is forget to create the folder – Aditia Pratama Oct 17 '17 at 14:18
  • It is also worth noting, that you will be renaming the file like that might cause some issues if it is not in fact a PNG file. – Kendas Oct 17 '17 at 14:19
  • The uploaded file is an image in png format. Will renaming the uploaded file to logo.png solve the problem? – jz22 Oct 17 '17 at 14:34
  • I'm pretty sure `/static/customlogos` folder is not present in your file system. Try replacing this with `/tmp` and see the result. – Andrejs Cainikovs Oct 17 '17 at 14:40
  • If I just use the path "/" the file is saved to C:\. However i want to save the file to my static folder that is located in the same folder like my python app. – jz22 Oct 17 '17 at 15:02

2 Answers2

6

Obviously, you want to save uploaded file as static/customlogos/logo.png, path that is relative to your Flask application directory, but you have specified the absolute non-existing path /static/customlogos.
Furthermore, according to your comments you are developing under Windows, which adds inconsistency to your question.

In any case, to achieve what you want, you need to know the absolute path of your application, and use it as a starting point:

latestfile.save(os.path.join(app.root_path, 'static/customlogos/logo.png'))

Cross-platform variant:

latestfile.save(os.path.join(app.root_path, 'static', 'customlogos', 'logo.png'))

Ninja dust-proof variant:

latestfile.save(os.path.join(app.root_path, app.config['STATIC_FOLDER'], 'customlogos', 'logo.png'))
Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
3

You can simplify the operation like below:

from flask import Flask, request, session, g, redirect
from flask import url_for, abort, render_template, flash, jsonify
import os

# Create two constant. They direct to the app root folder and logo upload folder
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static', 'customlogos')

# Configure Flask app and the logo upload folder
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

# In controller save the file with desired name
latestfile = request.files['customlogo']
full_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'logo.png')
latestfile.save(full_filename)

N.B.: Ensure that you have created customlogos in static folder.

arshovon
  • 13,270
  • 9
  • 51
  • 69