Answer the question
In order to leave comments, you need to log in
The file uploaded via flask is not saved, how to save?
I tried saving the file via the standard save function and via with open (see below)
db_sess = db_session.create_session()
try:
news = db_sess.query(News).get(news_id)
except Exception as ex:
print(ex)
return abort(404)
if request.method == "GET":
return render_template("edit_news.html", news=news)
form = request.form
file = request.files
if "img" in file.keys():
file = file["img"]
filename = file.filename
filetype = filename.rsplit(".", 1)[1]
if file and "." in filename and filetype in ALLOWED_IMAGES_TYPES:
filename = f'{datetime.datetime.now().strftime("%d.%m.%Y;%H:%M:%S")}.{filetype}'
file.save(os.path.join(UPLOAD_FOLDER, secure_filename(filename)))
news.img = filename
news.commit()
db_sess = db_session.create_session()
try:
news = db_sess.query(News).get(news_id)
except Exception as ex:
print(ex)
return abort(404)
if request.method == "GET":
return render_template("edit_news.html", news=news)
form = request.form
file = request.files
if "img" in file.keys():
file = file["img"]
filename = file.filename
filetype = filename.rsplit(".", 1)[1]
if file and "." in filename and filetype in ALLOWED_IMAGES_TYPES:
filename = f'{datetime.datetime.now().strftime("%d.%m.%Y;%H:%M:%S")}.{filetype}'
with open(os.path.join(UPLOAD_FOLDER, secure_filename(filename)), "w") as f:
f.write(file.read())
news.img = filename
news.commit()
Answer the question
In order to leave comments, you need to log in
I figured it out: the UPLOAD_FOLDER constant equal to "/static/pictures/" was used in the code. All you had to do was remove the / in front of the whole path
Check that all conditions are met.
file.save() takes a path to a folder (maybe a file) as parameters and saves it there. Check what os.path.join returns.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question